1

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:

Hierarchy table

enter image description here

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.

greengold
  • 1,184
  • 3
  • 18
  • 43
  • Is there any difference if you specify key columns name on your ID attributes? – Xavier Lambros Mar 06 '17 at 14:51
  • no, the same thing... – greengold Mar 06 '17 at 15:09
  • If you add @Basic(optional = false) on your IDs... – Xavier Lambros Mar 06 '17 at 15:17
  • the same... but IDE tells me then that I have "more than one attribute configured for field 'id'". jpa doesn't complaining through. – greengold Mar 06 '17 at 15:23
  • And if you try something like this @JoinColumn(name="hierarchy_fk", referencedColumnName="ID") on your @ManyToOne... – Xavier Lambros Mar 06 '17 at 15:25
  • no luck... I even tried to add 'referencedColumnName="ID"' to @JoinColumn, but still the same. – greengold Mar 06 '17 at 15:28
  • In https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing, "Identity sequencing requires the insert to occur before the id can be assigned, so it is not assigned on persist like other types of sequencing. You must either call commit() on the current transaction, or call flush() on the EntityManager. It may also be that you did not set your primary key column in your table to be an identity type." – Xavier Lambros Mar 06 '17 at 15:42
  • this is how I create the Hierarchy: public void createHierarchy(Hierarchy hierarchy) { entityManager.getTransaction().begin(); entityManager.persist(hierarchy); entityManager.flush(); entityManager.getTransaction().commit(); } I also tried @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; on Hierarchy entity but still the same... – greengold Mar 06 '17 at 15:48
  • And if you add @JoinColumn(name = "hierarchy_fk", insertable=false, updateable=false). This is my last idea... – Xavier Lambros Mar 06 '17 at 15:56
  • still no reference. It has to have something to do with Hierarchy's self referencing nature.. I have one-to-one relationship in this project that works... also what is the correct way of persisting of such a hierarchy? do I need to persist all of Hierarchy objects or I'm fine with persisting only root element that contains all other childs? I'm trying both, Hierarchy table looks the same through – greengold Mar 06 '17 at 16:02
  • Just have a look at http://stackoverflow.com/questions/1795649/jpa-persisting-a-one-to-many-relationship – Xavier Lambros Mar 06 '17 at 16:07
  • well I have resolved it by converting to many-to-many relationship as last comment suggests. But.. to me this doesn't really makes sence because it will be still one hierarchy that will have reference to all sorts of it's properties described as entities. (Folders in my case) – greengold Mar 06 '17 at 16:27
  • I agree with you. Manytomany makes no sense in your case. I think the first post (27 likes) is the correct answer. I can't help you anymore, perhaps, somebody else will have a better idea. – Xavier Lambros Mar 06 '17 at 16:36
  • nevermind I can move on with that at least. Thanks for your effort! – greengold Mar 06 '17 at 17:13
  • 1
    if you haven't resolved this yet, the mappings seem fine as presented in the question. This problem is commonly caused by 'not' setting both sides of your bidirectional relationship - your symptoms would occur if you are setting the 'many' side but not the owning side. This will cause the relationship to appear set in the many side for as long as that entity exists in the cache - once it is reloaded/refreshed, the collection will be empty, as the foreign key is only controlled through the owning side. JPA requires you to manage both sides of your relationships – Chris Mar 06 '17 at 17:33

0 Answers0