我有5个表,每个表与下一个表有一对多的关系 Project_t - > project_level1_t - > project_level2_t - > project_level3_t - > project_level4_t 我想用这些表来使用hibernate
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)
)
并且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
)
和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
这是存储库文件
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;
}
和
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;
}
}
和
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 ProjectLevel1Repository 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;
}
}
我想自己保存每个实体,所以如果运行此代码
ProjectRepository projectRepository = projectService.getProjectById(Long.parseLong(projectId));
ProjectLevel1Repository projectLevel1Repository = new ProjectLevel1Repository();
projectLevel1Repository.setProject(projectRepository);
projectLevel1Repository.setLevelName(level1Name);
projectLevel1Service.saveOrUpdateRepository(projectLevel1Repository);
ProjectLevel2Repository projectLevel2Repository = new ProjectLevel2Repository();
projectLevel2Repository.setLevelName("Test Level 2");
projectLevel2Repository.setProjectlevel1id(projectLevel1Repository);
projectLevel2Service.saveOrUpdateRepository(projectLevel2Repository);
我想保存一个projectLevel1和一个projectLevel2,但现在发生的事情是两个projectLevel1保存了一个projectLevel2