Posts

Showing posts from December, 2013

Hibernate Tutorial - Part V

For previous posts, refer: Hibernate tutorial Part - I Hibernate tutorial Part - II Hibernate tutorial Part - III Hibernate tutorial Part - IV In this post, we are going to take a look at ' Table per subclass' approach for inheritance. Table per subclass: This strategy, represents inheritance relationships as relational foreign key associations.Every class/subclass that declares persistent properties, including abstract classes and even interfaces has its own table. Unlike the table per concrete class strategy, the table here contains columns only for each non inherited property (each property declared by the subclass itself) along with a primary key that is also a foreign key of the super class table. Lets take a look at the class definitions and annotations: User Class: @Entity @Table(name = "USER") @Inheritance(strategy=InheritanceType.JOINED) public class User implements Serializable{ @Id @Column(name = "USER_ID&quo

Hibernate Tutorial - Part IV

For previous posts refer: Hibernate tutorial Part - I Hibernate tutorial Part - II Hibernate tutorial Part - III In this post, we will take a look at 'Table per class hierarchy' strategy of inheritance. Table Per Class hierarchy: In this approach, an entire class hierarchy can be mapped to a single table. This table includes columns  for all properties of all classes in the hierarchy. The concrete subclass represented by a particular row is identified by the value of a type discriminator  column. Lets take a look at the classes and their annotations: User Class: @Entity @Table(name = "USER") @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn( name="USER_TYPE", discriminatorType=DiscriminatorType.STRING ) @DiscriminatorValue(value="U") public class User implements Serializable{ @Id @Column(name = "USER_ID") private String userId; @Column(name = "USERNAME&q

Hibernate Tutorial - Part III

For previous parts refer: Hibernate tutorial Part - I Hibernate tutorial Part - II Inheritance Mapping: In this post, I'm going to write about Inheritance mapping in Hibernate. Essentially, there are 3 different types of inheritance mapping which could be achieved: Table per concrete class  Table per class hierarchy Table per subclass 1. Table per concrete class In this type of mapping, we will have a table mapped to every concrete class in the hierarchy. Lets say we have the following class hierarchy for representing different users of the system. Abstract class, User Concrete subclass, Customer Concrete subclass, InternalUser Lets take a look at the class definitions and the the annotations for each of these classes. User Class: @Entity @Table(name = "USER") @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class User implements Serializable{ @Id @Column(name = "USER_ID") private String u