Created
April 29, 2014 12:40
-
-
Save tkt028/11399071 to your computer and use it in GitHub Desktop.
SpringJPA: map Java inheritance hierarchies to database tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @Note: Sample code from Broadleaf | |
@Entity | |
@Inheritance(strategy = InheritanceType.JOINED) | |
@Table(name = "BLC_CUSTOMER", uniqueConstraints = @UniqueConstraint(columnNames = { "USER_NAME" })) | |
public class CustomerImpl implements Customer, AdminMainEntity { | |
@Id | |
@Column(name = "CUSTOMER_ID") | |
protected Long id; | |
@Column(name = "USER_NAME") | |
protected String username; | |
@Column(name = "PASSWORD") | |
protected String password; | |
// ........ | |
} | |
// ================================================================= | |
// @Note: in the new table `WEBA_CUSTOMER_EXT`, we must add a column `CUSTOMER_ID` to play | |
// as a foreign key to reference back to the table `BLC_CUSTOMER` of supperclass CustomerImpl | |
@Entity | |
@Table(name = "WEBA_CUSTOMER_EXT") | |
public class WebaCustomerEntity extends CustomerImpl { | |
public WebaCustomerEntity() { | |
System.out.println("TKT - constructor of WebaCustomerEntity()"); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Is it possible to build a JPA entity by extending a POJO? | |
// - http://stackoverflow.com/questions/2516329/is-it-possible-to-build-a-jpa-entity-by-extending-a-pojo | |
JPA specification states | |
- Entities may extend non-entity classes as well as entity classes, | |
and non-entity classes may extend entity classes. | |
@javax.persistence.MappedSuperclass annotation allows you to define | |
this kind of mapping | |
// ================================================================= | |
@MappedSuperclass | |
public class MyThing implements Serializable { | |
private int myNumber; | |
private String myData; | |
// getter's and setter's | |
} | |
And | |
@Entity | |
@Table(name="MY_THING") | |
public class MyThingEntity extends MyThing { | |
} | |
// ================================================================= | |
As said by JPA specification | |
- The MappedSuperclass annotation designates a class whose mapping | |
information is applied to the entities that inherit from it. | |
And | |
+ A class designated with the MappedSuperclass annotation can be | |
mapped in the same way as an entity except that the mappings | |
will apply only to its subclasses since no table exists for | |
the mapped superclass itself. | |
+ If you need to override some property defined by MyThing, use | |
@AttibuteOverride (when you want to override a single property) | |
or @AttibuteOverrides (when you want to override more than one | |
property) | |
// ================================================================= | |
@Entity | |
@Table(name="MY_THING") | |
@AttributeOverride(name="myData", column=@Column(name="MY_DATA")) | |
public class MyThingEntity extends MyThing { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JPA Implementation Patterns: Mapping Inheritance Hierarchies | |
// - http://java.dzone.com/articles/jpa-implementation-patterns-mapping | |
// - http://stackoverflow.com/questions/2516329/is-it-possible-to-build-a-jpa-entity-by-extending-a-pojo | |
JPA provides three ways to map Java inheritance hierarchies to | |
database tables: | |
- InheritanceType.SINGLE_TABLE - The whole inheritance hierarchy is | |
mapped to one table. An object is stored in exactly one row in that | |
table and the discriminator value stored in the discriminator column | |
specifies the type of the object. | |
- InheritanceType.TABLE_PER_CLASS - Every concrete entity class in the | |
hierarchy is mapped to a separate table. An object is stored in | |
exactly one row in the specific table for its type. | |
- InheritanceType.JOINED - Every class in the hierarchy is represented | |
as a separate table, causing no field duplication to occur. An | |
object is stored spread out over multiple tables ; one row in each | |
of the tables that make up its class inheritance hierarchy. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment