开发者

Bidirectional relationship with superclass entity property in JPA

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.

Both test and folder have a Node superclass, look开发者_高级运维s like this:

@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...
}

And here is the Folder class:

@Entity
public class Folder extends Node{

    @LazyCollection(LazyCollectionOption.FALSE) 
    @OneToMany(cascade=CascadeType.ALL, **mappedBy="parent"**)
    List<Folder> folders;

    @LazyCollection(LazyCollectionOption.FALSE) 
    @OneToMany(cascade=CascadeType.ALL, **mappedBy="parent"**)
    List<Test> tests;

    ...
}   

So the main problem is the mappedBy property, which relates to superclass property not overriden in ancestor, cause I got such exception:

Exception while preparing the app : mappedBy reference an unknown target entity property: my.test.model.Folder.parent in my.test.model.Folder.folders

There is might be some tricky mapping over "folders" and "test" properties of Folder class, and I need a little help with that.

EDIT: I specified both folders and tests properties of Folder class with targetEntity=Node.class:

    @LazyCollection(LazyCollectionOption.FALSE) 
    @OneToMany(cascade=CascadeType.ALL, mappedBy="parent",targetEntity=Node.class)
    List<Folder> folders;

    @LazyCollection(LazyCollectionOption.FALSE) 
    @OneToMany(cascade=CascadeType.ALL, mappedBy="parent",targetEntity=Node.class)
    List<Test> tests;

And its gets work. But works not fine. Now both tests and folders mapped to both properties (I dont know why I'm not geting exceptions), when I need to get them separately.

So I'm still looking for a suitable mappings to achieve that. I would appriciate any help.


Interesting structure, probably contains many challenges. But to the question about relationship to superclass entitys property: unfortunately you cannot do that with Hibernate. Your approach to set targetEntityClass also does not work, because it effectively changes type of collection elements from entity mapping point of view.

Luckily there is workaround. You can have relationship from entity to the property of MappedSuperClass, which you extend. Likely you do not want to make Node to be MappedSuperClass, so you have to add new. And then it have to be located between Node and Folder: Folder->MappedSuperClass->Node. It goes like this (such a approach works at least with Hibernate 3.5.6, no idea since which version it is supported):

public class Node {
      ... //without parent attribute, otherwise as before
}

@MappedSuperclass
public class SomethingWithParent extends Node {
    @ManyToOne
    Node parent;
}
public class Folder extends SomethingWithParent {
..
//as before
}

public class Test extends SomethingWithParent {
...//as before
}

By the way, isn't Node bit too generic for Parent? At least if it is used only for this purpose. All parents are folders.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜