多层次继承的jpa注释是什么样的

时间:2019-05-02 07:44:19

标签: java jpa eclipselink

嗨,我有一个抽象类TreeNode和一个从TreeNode继承的抽象类TreeNodeImage。由于并非所有实体都需要图像,因此某些实体直接从TreeNode继承,而某些实体则从TreeNodeImage继承。 Diagram

现在我有另一个实体User和一个MappedUserTreenode类,其中每个用户都映射有一个TreeNode。我的问题是,如果我想使用User.getMappedUserTreeNode()获取所有MappedUserTreeNodes,那么它给我一个不直接从TreeNode继承的对象的null。

TreeNode类

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class TreeNode extends Observable {

@OneToMany(cascade = { CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH }, orphanRemoval = true, fetch = FetchType.LAZY)
@JoinColumn(foreignKey = @ForeignKey(name = "TREENODE_ID"))
private Set<MappedUserTreeNode> mappedUserTreeNode;

用户类别:

@Entity
@Table(name = "user")
@NamedQueries({ @NamedQuery(name = "user.findAll", query = "SELECT u FROM User u order by u.firstName") })
public class User implements Serializable {

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinColumn(name = "USER_ID", referencedColumnName = "id")
private Set<MappedUserTreeNode> mappedUserTreeNodes = new HashSet<MappedUserTreeNode>();

MappedUserTreeNode类:

@Entity
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "TREENODE_ID", "USER_ID"}) }, name = "mappedusertreenode")
public class MappedRole implements Serializable {
@ManyToOne(cascade = { CascadeType.REMOVE }, fetch = FetchType.EAGER)
@JoinColumn(name = "TREENODE_ID", nullable = false)
private TreeNode treeNode;

TreeNodeImage类:

@Entity 
public abstract class TreeNodeImage extends TreeNode {

我可以用JPA解决这个问题吗?还是必须更改班级结构?

0 个答案:

没有答案