Belal Othman Belal Othman - 2 months ago 20
Java Question

Unable to find column with logical name

I have 5 tables every table has relation one to many with the next table

Project_t -> project_level1_t -> project_level2_t -> project_level3_t -> project_level4_t

I want to user hibernate with these tables

CREATE TABLE project_t
(
projectid serial NOT NULL,
address1 character varying(128),
postcode character varying(7),
city character varying(64),
level_four_name character varying(128),
CONSTRAINT project_t_pkey PRIMARY KEY (projectid),
CONSTRAINT projectcode_unique UNIQUE (projectcode)
)


And project_level1_t

CREATE TABLE project_level1_t
(
projectlevel1id integer NOT NULL DEFAULT nextval('project_level1_t_projectlevel1pk_seq'::regclass),
levelname character varying(256),
projectid integer,
CONSTRAINT project_level1_t_pkey PRIMARY KEY (projectlevel1id),
CONSTRAINT project_level1_t_project_t_fkey FOREIGN KEY (projectid)
REFERENCES project_t (projectid) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)


And project_level2_t

CREATE TABLE project_level2_t
(
projectlevel2id integer NOT NULL DEFAULT nextval('project_level2_t_projectlevel2_seq'::regclass),
levelname character varying(256),
projectlevel1id integer,
CONSTRAINT project_level2_t_pkey PRIMARY KEY (projectlevel2id),
CONSTRAINT project_level2_t_projec_level_1_fkey FOREIGN KEY (projectlevel1id)
REFERENCES project_level1_t (projectlevel1id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION


And this is the Repository files

package ma.eurnet.mp.tables.model;

import javax.persistence.GenerationType;

@JsonAutoDetect
@Entity
@Table(name = "project_t")
public class ProjectRepository implements Serializable {

private static final long serialVersionUID = 1L;

public ProjectRepository() {
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "projectid")
private Long projectId;

@OneToMany(fetch = FetchType.EAGER, mappedBy="project")
@JsonIgnore
private List<ProjectLevel1Repository> projectlevel1 = new ArrayList<ProjectLevel1Repository>();

public Long getProjectId() {
return projectId;
}

public void setProjectId(Long projectId) {
this.projectId = projectId;
}

public List<ProjectLevel1Repository> getProjectlevel1() {
return projectlevel1;
}

public void setProjectlevel1(List<ProjectLevel1Repository> projectlevel1) {
this.projectlevel1 = projectlevel1;
}


and

package ma.eurnet.mp.tables.model;

@JsonAutoDetect
@Entity
@SequenceGenerator(name = "projectLevel1Sequence", sequenceName = "project_level1_t_projectlevel1pk_seq", allocationSize = 1)
@Table(name="project_level1_t")
public class ProjectLevel1Repository implements Serializable {

private static final long serialVersionUID = 1L;

public ProjectLevel1Repository() {
}

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "projectLevel1Sequence")
@Column(name = "projectlevel1id")
private Long projectLevel1Id;

@ManyToOne
@JoinColumn(name="projectid", referencedColumnName = "projectid")
@JsonIgnore
private ProjectRepository project;

@OneToMany(fetch = FetchType.EAGER, mappedBy="projectlevel1id")
@JsonIgnore
private List<ProjectLevel2Repository> projectlevel2 = new ArrayList<ProjectLevel2Repository>();

public List<ProjectLevel2Repository> getProjectlevel2() {
return projectlevel2;
}

public Long getProjectLevel1Id() {
return projectLevel1Id;
}


public void setProjectLevel1Id(Long projectLevel1Id) {
this.projectLevel1Id = projectLevel1Id;
}


public ProjectRepository getProject() {
return project;
}


public void setProject(ProjectRepository project) {
this.project = project;
}




}


And

package ma.eurnet.mp.tables.model;

import java.io.Serializable;


@JsonAutoDetect
@Entity
@SequenceGenerator(name = "projectLevel2Sequence", sequenceName = "project_level2_t_projectlevel2_seq", allocationSize = 1)
@Table(name="project_level2_t")
public class ProjectLevel2Repository implements Serializable {

private static final long serialVersionUID = 1L;

public ProjectLevel2Repository() {
}

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "projectLevel2Sequence")
@Column(name = "projectlevel2id")
private Long projectLevel2Id;

@Column(name = "levelname")
private String levelName;


@Column(name = "created_by")
private String createdBy;

@Column(name = "creation_date")
private String creationDate;


@ManyToOne
@JoinColumn(name="projectlevel1id", referencedColumnName = "projectlevel1id")
@JsonIgnore
private ProjectRepository projectlevel1id;

public Long getProjectLevel2Id() {
return projectLevel2Id;
}


public void setProjectLevel2Id(Long projectLevel2Id) {
this.projectLevel2Id = projectLevel2Id;
}


public String getLevelName() {
return levelName;
}


public void setLevelName(String levelName) {
this.levelName = levelName;
}


public String getCreatedBy() {
return createdBy;
}


public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}


public String getCreationDate() {
return creationDate;
}


public void setCreationDate(String creationDate) {
this.creationDate = creationDate;
}


public ProjectRepository getProjectlevel1id() {
return projectlevel1id;
}


public void setProjectlevel1id(ProjectRepository projectlevel1id) {
this.projectlevel1id = projectlevel1id;
}



}


but when I run my application I get this error

org.hibernate.MappingException: Unable to find column with logical name: projectlevel1id in org.hibernate.mapping.Table(project_t) and its related supertables and secondary tables

how can I fix this please

Answer

In ProjectLevel2Repository you have a mapping towards ProjectRepository using a @JoinColumn named projectlevel1id which doesn't exist. From what you describe, this should be mapped to ProjectLevel1Repository

@ManyToOne
@JoinColumn(name="projectlevel1id", referencedColumnName = "projectlevel1id")
@JsonIgnore
private ProjectLevel1Repository projectlevel1;
Comments