Note that there are some explanatory texts on larger screens.

plurals
  1. POBidirectional relationship with superclass entity property in JPA
    text
    copied!<p>I'm tying to implement some tree-like structure with JPA. I have a "folder" entity and a "test" entity. Folder can contain both folders and tests. Test doesnt contains anything.</p> <p>Both test and folder have a Node superclass, looks like this:</p> <pre><code>@Entity @Inheritance(strategy = InheritanceType.JOINED) public class Node implements TreeNode, Serializable{ private Long id; String description; String name; @ManyToOne Node parent; ...getters, setters and other stuff that doesnt matter... } </code></pre> <p>And here is the Folder class:</p> <pre><code>@Entity public class Folder extends Node{ @LazyCollection(LazyCollectionOption.FALSE) @OneToMany(cascade=CascadeType.ALL, **mappedBy="parent"**) List&lt;Folder&gt; folders; @LazyCollection(LazyCollectionOption.FALSE) @OneToMany(cascade=CascadeType.ALL, **mappedBy="parent"**) List&lt;Test&gt; tests; ... } </code></pre> <p>So the main problem is the mappedBy property, which relates to superclass property not overriden in ancestor, cause I got such exception:</p> <pre><code>Exception while preparing the app : mappedBy reference an unknown target entity property: my.test.model.Folder.parent in my.test.model.Folder.folders </code></pre> <p>There is might be some tricky mapping over "folders" and "test" properties of Folder class, and I need a little help with that. </p> <p>EDIT: I specified both folders and tests properties of Folder class with targetEntity=Node.class:</p> <pre><code> @LazyCollection(LazyCollectionOption.FALSE) @OneToMany(cascade=CascadeType.ALL, mappedBy="parent",targetEntity=Node.class) List&lt;Folder&gt; folders; @LazyCollection(LazyCollectionOption.FALSE) @OneToMany(cascade=CascadeType.ALL, mappedBy="parent",targetEntity=Node.class) List&lt;Test&gt; tests; </code></pre> <p>And its gets work. But works not fine. Now both tests and folders mapped to <strong>both</strong> properties (I dont know why I'm not geting exceptions), when I need to get them separately. </p> <p>So I'm still looking for a suitable mappings to achieve that. I would appriciate any help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload