JPA TABLE_PER_CLASS Inheritance : abstract entity table created

1.4k views Asked by At

I have created 3 Entity classes named NodeBase , NodeDocument and NodeFolder .

And using Table Per Class Inheritance stategy and as defined in table per class inheritance a table is defined for each concrete class in the inheritance hierarchy , in this case NodeBase is abstract entity but the table of this entity is created named NODEBASE .

Entity relationship diagram

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class NodeBase implements Serializable {

    @Column(name="NBS_UUID")
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long uuid;

    @Column(name="NBS_NAME")
    @Basic
    private String name;

    @ManyToOne(targetEntity = NodeFolder.class)
    private NodeFolder parent;

}


@Entity
@Table(name="SDM_NODE_DOCUMENT")
public class NodeDocument extends NodeBase implements Serializable {

    @Column(name="NDC_MIMETYPE")
    @Basic
    private String mimeType;

}

@Entity
@Table(name="SDM_NODE_FOLDER")
public class NodeFolder extends NodeBase implements Serializable {

    @OneToMany(targetEntity = NodeBase.class,mappedBy = "parent")
    private Collection<NodeBase> children;
}

How to create entity classes , in such a way that only two tables should be created named SDM_NODE_DOCUMENT and SDM_NODE_FOLDER ?

enter image description here

2

There are 2 answers

0
Ismael_Diaz On

eclipse link is reported to do that,at least in earlier versions, some people says it is a bug, or maybe not, just a table generated by eclipselink to do the union of all the tables of the inheritance.

If the parent class is abstract you can use @MappedSupperclass instead of @Entity anotation to avoid the creation of the table if you are not quering the abstract class

0
amit kumar On
@MappedSuperclass
public abstract class NodeBase implements Serializable {

    @Column(name="NBS_UUID")
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long uuid;

    @Column(name="NBS_NAME")
    @Basic
    private String name;

    @ManyToOne(targetEntity = NodeFolder.class)
    private NodeFolder parent;

}

Now it works. Go with it.