Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherited abstract class with JPA (+Hibernate)

How would you configure annotations in the following example code? I'd like to stick with JPA annotations only and avoid Hibernate specific dependencies. Is the code below correct?

@Entity public class RefExample extends RefData {  } 

(There will be multiple versions of these classes, RefSomeOtherExample, etc, and one db table per class. Some may add additional fields (columns) but most will simply make use of the basic fields inherited from the "RefData" base class.)

Base class:

@Entity public abstract class RefData {      private long id;     private String code;     private String desc;      @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     @Column(unique = true, nullable = false)     public long getId() {          return id;     }      public void setId(long id) {          this.id = id;     }      @Column(unique = true, nullable = false, length=8)     public String getCode() {          return code;     }      public void setCode(String code) {          this.code = code;     }      @Column(unique = true, nullable = false, length=80)     public String getDesc() {          return desc;     }      public void setDesc(String desc) {          this.desc = desc;     } } 

Ultimately I'd like to generate schema creation scripts from this using Hibernate's SchemaExport class. In the case above these two classes should only result in the creation of a single table named "RefExample" with the three columns from "RefData". Will this work?

like image 433
Manius Avatar asked Sep 30 '10 03:09

Manius


People also ask

Can we use JPA with Hibernate?

Hibernate is an implementation of JPA. Hence, the common standard which is given by JPA is followed by Hibernate. It is a standard API that permits to perform database operations. It is used in mapping Java data types with SQL data types and database tables.

Which inheritance strategy is better in Hibernate?

If you have a single hierarchy, prefer To use Single Table Inheritance strategy. Joined strategy is better designed when you have a complex hierarchy but it suffers due To performance issues when querying for any entity. Save this answer.

Should MappedSuperclass be abstract?

Mapped superclasses cannot be queried and can't be used in EntityManager or Query operations. You must use entity subclasses of the mapped superclass in EntityManager or Query operations. Mapped superclasses can't be targets of entity relationships. Mapped superclasses can be abstract or concrete.

How does JPA inheritance work?

JPA Inheritence Overview Inheritence is a key feature of object-oriented programming language in which a child class can acquire the properties of its parent class. This feature enhances reusability of the code. The relational database doesn't support the mechanism of inheritance.


2 Answers

From JPA 1.0 specification:

Both abstract and concrete classes can be entities. Both abstract and concrete classes can be annotated with the Entity annotation, mapped as entities, and queried for as entities.

Entities can extend non-entity classes and non-entity classes can extend entity classes.

As you want a single table, you should use Single Table inheritance.

Just define a discriminator column as follows:

@Entity @DiscriminatorColumn(name="REF_TYPE") public abstract class RefData { 

But if you do not want to rely on JPA inheritance strategies, you can use MappedSuperclass instead:

@MappedSuperclass public abstract class RefData { 

JPA specification

An entity may inherit from a superclass that provides persistent entity state and mapping information, but which is not itself an entity. Typically, the purpose of such a mapped superclass is to define state and mapping information that is common to multiple entity classes.

Keep in mind you can not use @Entity and @MappedSuperclass at the same time.

like image 80
Arthur Ronald Avatar answered Oct 07 '22 13:10

Arthur Ronald


@MappedSuperclass is worked for me. I was struggling to map a view to 2 objects which are Parent and child classes. My view is joined from 2 tables. Primary keys from both the tables are present in the view. @DiscriminatorColumn is not worked for me since it requires a column exclusively allotted to data type of the object and also it is throwing 'repeated Column in object exception' which I could not solve.

I read this forum and I tried @MappedSuperclass annotation. It does the trick.

I've put @MappedSuperclass at the superclass and put the @Id and @GeneratedValue in the superclass identifier. In the sublass I've given as

@Entity @Table(name="view_name") 

and used sub class object to fetch the data from view. That's it.

Inheritance in hibernate annotations for Joined table with out using @DiscriminatorColumn worked for me.

like image 42
Amirtharaj Avatar answered Oct 07 '22 14:10

Amirtharaj