I am trying to model one-to-many bidirectional relationship between two entities. Entity on 'one' side is self-containing used to model a hierarchy. Each hierarchy should have a collection of entities on the 'many' owning side.
I have managed to model this self-containment, but I can not get one-to-many relationship working. What is happening is that foreign keys are null.
this are the entities:
Hierarchy
public class Hierarchy {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
@OneToMany(mappedBy = "hierarchy", cascade = CascadeType.ALL)
private List<Folder> folders;
@Column(name = "paramName")
private String paramName;
@Column(name = "hierarchyType")
@Enumerated(EnumType.STRING)
private EHierarchyType hierarchyType;
@OneToOne
@JoinColumn(name = "parent_id")
private Hierarchy parent;
@OneToOne(mappedBy = "parent", cascade = CascadeType.ALL)
private Hierarchy child;
Folder
public class Folder {
@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE)
private long id;
@ManyToOne
@JoinColumn(name = "hierarchy_fk")
private Hierarchy hierarchy;
@Column(name = "name")
private String name;
@ElementCollection
@Column(name = "keywords")
@CollectionTable(name = "Folder_keywords")
private Set<String> keywords;
How it looks in the db:
So you see 'hierarchy_fk' in 'Folder' table is null.
Also it is very interesting that, when I persist initialized hierarchies with folders set and I do a query right after persist, it returns me correct hierarchy with all folders set on each level. But when I try to read hierarchies in some other test method, folders are not set.
Question is why is all this happening and how to properly fix this?
I'm using Eclipselink 2.6.1 on Mysql DB.