休眠 @OneToMany / @ManyToOne 列没有出现
Hibernate @OneToMany / @ManyToOne column not appearing
我有一个 @OneToMany
/ @ManyToOne
关系,其中 Project
可以有一个或多个 Phases
而 Phase
取决于 Project
。很简单。
这是我的实体(相关部分):
@Entity
@Table(name = "project")
public class ProjectEntity {
@Id
@Column(name = "project_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
// Our Set of phases to be saved in the database as an array
@OneToMany(targetEntity = PhaseEntity.class)
private Set<PhaseEntity> phases = new HashSet<>(0);
// Getters and setters
}
还有我的PhaseEntity
:
@Entity
@Table(name = "phase")
public class PhaseEntity {
@Id
@Column(unique = true, name="phase_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
// Our project on which the phases are appart of
@NotNull
@ManyToOne(targetEntity = ProjectEntity.class)
private ProjectEntity project;
// Getters and setters
}
在现有项目中创建阶段后,我通过创建实体和
将它们一一保存
phaseDao.save(phaseEntity);
最后像这样在我的项目中保存阶段集:
project.setPhases(phaseSet);
我的阶段已正确创建并已 link 编辑到项目中,但我的项目没有显示任何阶段迹象,因为它们没有 phases
列。
编辑:事实证明 Hibernate 创建了一个名为 project_phases
的 link table,有没有一种方法可以在不创建 linktable?
我哪里不明白?
在@OneToMany注解中使用mappedBy属性
@OneToMany(targetEntity = PhaseEntity.class, mappedby="project")
private Set<PhaseEntity> phases = new HashSet<>(0);
我有一个 @OneToMany
/ @ManyToOne
关系,其中 Project
可以有一个或多个 Phases
而 Phase
取决于 Project
。很简单。
这是我的实体(相关部分):
@Entity
@Table(name = "project")
public class ProjectEntity {
@Id
@Column(name = "project_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
// Our Set of phases to be saved in the database as an array
@OneToMany(targetEntity = PhaseEntity.class)
private Set<PhaseEntity> phases = new HashSet<>(0);
// Getters and setters
}
还有我的PhaseEntity
:
@Entity
@Table(name = "phase")
public class PhaseEntity {
@Id
@Column(unique = true, name="phase_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
// Our project on which the phases are appart of
@NotNull
@ManyToOne(targetEntity = ProjectEntity.class)
private ProjectEntity project;
// Getters and setters
}
在现有项目中创建阶段后,我通过创建实体和
将它们一一保存phaseDao.save(phaseEntity);
最后像这样在我的项目中保存阶段集:
project.setPhases(phaseSet);
我的阶段已正确创建并已 link 编辑到项目中,但我的项目没有显示任何阶段迹象,因为它们没有 phases
列。
编辑:事实证明 Hibernate 创建了一个名为 project_phases
的 link table,有没有一种方法可以在不创建 linktable?
我哪里不明白?
在@OneToMany注解中使用mappedBy属性
@OneToMany(targetEntity = PhaseEntity.class, mappedby="project")
private Set<PhaseEntity> phases = new HashSet<>(0);