Hibernate : Mapping inheritance hierarchy

Hibernate has three distinct ways to map an inheritance
relationship

  • Use one table for each concrete (nonabstract) class
  • Use one table for each class hierarchy
  • Use one table for each subclass, including interfaces and
    abstract classes

Use one table for each concrete (nonabstract) class

		@Entity
		@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
		public class Student implements Serializable {
			...
		}
	


Use one table for each class hierarchy

		@Entity
		@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
		@DiscriminatorColumn(
		name="PERSON_TYPE",
		discriminatorType=STRING
		)
		@DiscriminatorValue("PE")
		public class Person implements Serializable {
		...
		}
		
@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorValue("ST") public class Student extends Person{ ... }

One table per subclass

		@Entity
		@Inheritance(strategy=InheritanceType.JOINED)
		public class Person implements Serializable {
		...
		}
		@Entity
		public class Student extends Person{
		...
		}	
		
// If tables are joined by columns with different names @Entity @PrimaryKeyJoinColumn(name="STUDENT_ID") public class Student extends Person{ ... }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: