I have a @OneToMany
/ @ManyToOne
relationship, where a Project
can have one or many Phases
and a Phase
depends on a Project
. Pretty simple.
Here are my entities (the relevant parts):
@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
}
And my 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
}
Upon creating phases in an already existing project, I save them one by one by creating an entity and
phaseDao.save(phaseEntity);
And finally save the set of phases in my project like such:
project.setPhases(phaseSet);
My phases ARE correctly created and are linked to a project but my projects do not show any sign of phases as they do not have a phases
column.
EDIT: Turns out that Hibernate creates a link table called project_phases
, is there a way to save an array of phases without creating a link table?
What am I not understanding?
use mappedBy attribute in @OneToMany Annotation
@OneToMany(targetEntity = PhaseEntity.class, mappedby="project")
private Set<PhaseEntity> phases = new HashSet<>(0);