Hibernate Inheritance: Table Per Subclass (Annotation & XML mapping)

Welcome to Hibernate Tutorial Series. In previous tutorials we saw how to implement Inheritance in Hibernate: One Table Per Hierarchy.

Today we will see how to implement Hibernate Inheritance: One Table per Subclass scheme.

Introduction to Inheritance in Hibernate

Java is an object oriented language. It is possible to implement Inheritance in Java. Inheritance is one of the most visible facets of Object-relational mismatch. Object oriented systems can model both “is a” and “has a” relationship. Relational model supports only “has a” relationship between two entities. Hibernate can help you map such Objects with relational tables. But you need to choose certain mapping strategy based on your needs.

One Table Per Subclass example

Suppose we have a class Person with subclass Employee and Owner. Following the class diagram and relationship of these classes.

hibernate-table-per-subclass-class-diagram-uml

In One Table per Subclass scheme, each class persist the data in its own separate table. Thus we have 3 tables; PERSON, EMPLOYEE and OWNER to persist the class data. Note that a foreign key relationship exists between the subclass tables and super class table. Thus the common data is stored in PERSON table and subclass specific fields are stored in EMPLOYEE and OWNER tables.

Following are the advantages and disadvantages of One Table per Subclass scheme.

Advantage

  • Using this hierarchy, does not require complex changes to the database schema when a single parent class is modified.
  • It works well with shallow hierarchy.

Disadvantage

  • As the hierarchy grows, it may result in poor performance.
  • The number of joins required to construct a subclass also grows.

Create Database Table to persist Subclass

CREATE TABLE `person` ( `person_id` BIGINT(20) NOT NULL AUTO_INCREMENT, `firstname` VARCHAR(50) NOT NULL DEFAULT '0', `lastname` VARCHAR(50) NOT NULL DEFAULT '0', PRIMARY KEY (`person_id`) ) CREATE TABLE `employee` ( `person_id` BIGINT(10) NOT NULL, `joining_date` DATE NULL DEFAULT NULL, `department_name` VARCHAR(50) NULL DEFAULT NULL, PRIMARY KEY (`person_id`), CONSTRAINT `FK_PERSON` FOREIGN KEY (`person_id`) REFERENCES `person` (`person_id`) ) CREATE TABLE `owner` ( `person_id` BIGINT(20) NOT NULL DEFAULT '0', `stocks` BIGINT(11) NULL DEFAULT NULL, `partnership_stake` BIGINT(11) NULL DEFAULT NULL, PRIMARY KEY (`person_id`), CONSTRAINT `FK_PERSON2` FOREIGN KEY (`person_id`) REFERENCES `person` (`person_id`) )
Code language: SQL (Structured Query Language) (sql)

Hibernate Inheritance: XML Mapping

Following is the example where we map Person, Employee and Owner entity classes using XML mapping.

File: Person.java

package net.viralpatel.hibernate; public class Person { private Long personId; private String firstname; private String lastname; // Constructors and Getter/Setter methods, }
Code language: Java (java)

File: Employee.java

package net.viralpatel.hibernate; import java.util.Date; public class Employee extends Person { private Date joiningDate; private String departmentName; // Constructors and Getter/Setter methods, }
Code language: Java (java)

File: Owner.java

package net.viralpatel.hibernate; public class Owner extends Person { private Long stocks; private Long partnershipStake; // Constructors and Getter/Setter methods, }
Code language: Java (java)

File: Person.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="net.viralpatel.hibernate"> <class name="Person" table="PERSON"> <id name="personId" column="PERSON_ID"> <generator class="native" /> </id> <property name="firstname" /> <property name="lastname" column="lastname" /> <joined-subclass name="Employee" extends="Person"> <key column="person_id" /> <property name="departmentName" column="department_name" /> <property name="joiningDate" type="date" column="joining_date" /> </joined-subclass> <joined-subclass name="Owner" extends="Person"> <key column="person_id" /> <property name="stocks" column="stocks" /> <property name="partnershipStake" column="partnership_stake" /> </joined-subclass> </class> </hibernate-mapping>
Code language: Java (java)

Note that we have defined only one hibernate mapping (hbm) file Person.hbm.xml. Both Person and Employee model class are defined within one hbm file.

<discriminator> tag is used to define the discriminator column.

<subclass> tag is used to map the subclass Employee. Note that we have not used the usual <class> tag to map Employee as it falls below in the hierarchy tree.

The discriminator-value for Person is defined as “P” and that for Employee is defined “E”, Thus, when Hibernate will persist the data for person or employee it will accordingly populate this value.

Hibernate Inheritance: Annotation Mapping

Following is the example where we map Employee and Person entity classes using JPA Annotations.

File: Person.java

package net.viralpatel.hibernate; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.Table; @Entity @Table(name = "PERSON") @Inheritance(strategy=InheritanceType.JOINED) public class Person { @Id @GeneratedValue @Column(name = "PERSON_ID") private Long personId; @Column(name = "FIRSTNAME") private String firstname; @Column(name = "LASTNAME") private String lastname; public Person() { } public Person(String firstname, String lastname) { this.firstname = firstname; this.lastname = lastname; } // Getter and Setter methods, }
Code language: Java (java)

The Person class is the root of hierarchy. Hence we have used some annotations to make it as the root.

@Inheritance – Defines the inheritance strategy to be used for an entity class hierarchy. It is specified on the entity class that is the root of the entity class hierarchy.

@InheritanceType – Defines inheritance strategy options. JOINED is a strategy in which fields that are specific to a subclass are mapped to a separate table than the fields that are common to the parent class, and a join is performed to instantiate the subclass. Thus fields of Employee (joining_date, department) and Owner (stocks etc) are mapped to their respective tables.

File: Employee.java

package net.viralpatel.hibernate; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Table; @Entity @Table(name="EMPLOYEE") @PrimaryKeyJoinColumn(name="PERSON_ID") public class Employee extends Person { @Column(name="joining_date") private Date joiningDate; @Column(name="department_name") private String departmentName; public Employee() { } public Employee(String firstname, String lastname, String departmentName, Date joiningDate) { super(firstname, lastname); this.departmentName = departmentName; this.joiningDate = joiningDate; } // Getter and Setter methods, }
Code language: Java (java)

File: Owner.java

package net.viralpatel.hibernate; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Table; @Entity @Table(name="OWNER") @PrimaryKeyJoinColumn(name="PERSON_ID") public class Owner extends Person { @Column(name="stocks") private Long stocks; @Column(name="partnership_stake") private Long partnershipStake; public Owner() { } public Owner(String firstname, String lastname, Long stocks, Long partnershipStake) { super(firstname, lastname); this.stocks = stocks; this.partnershipStake = partnershipStake; } // Getter and Setter methods, }
Code language: Java (java)

Both Employee and Owner classes are child of Person class. Thus while specifying the mappings, we used @PrimaryKeyJoinColumn to map it to parent table.

@PrimaryKeyJoinColumn – This annotation specifies a primary key column that is used as a foreign key to join to another table.

It is used to join the primary table of an entity subclass in the JOINED mapping strategy to the primary table of its superclass; it is used within a SecondaryTable annotation to join a secondary table to a primary table; and it may be used in a OneToOne mapping in which the primary key of the referencing entity is used as a foreign key to the referenced entity.

If no PrimaryKeyJoinColumn annotation is specified for a subclass in the JOINED mapping strategy, the foreign key columns are assumed to have the same names as the primary key columns of the primary table of the superclass

Main class

package net.viralpatel.hibernate; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; public class Main { public static void main(String[] args) { SessionFactory sf = HibernateUtil.getSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); Person person = new Person("Steve", "Balmer"); session.save(person); Employee employee = new Employee("James", "Gosling", "Marketing", new Date()); session.save(employee); Owner owner = new Owner("Bill", "Gates", 300L, 20L); session.save(owner); session.getTransaction().commit(); session.close(); } }
Code language: Java (java)

The Main class is used to persist Person, Employee and Owner object instances. Note that these classes are persisted in different tables.

Output

Hibernate: insert into PERSON (firstname, lastname) values (?, ?) Hibernate: insert into PERSON (firstname, lastname) values (?, ?) Hibernate: insert into Employee (department_name, joining_date, person_id) values (?, ?, ?) Hibernate: insert into PERSON (firstname, lastname) values (?, ?) Hibernate: insert into Owner (stocks, partnership_stake, person_id) values (?, ?, ?)
Code language: SQL (Structured Query Language) (sql)
hibernate-inheritance-table-per-subclass-table-output

Download Source Code

Hibernate-inheritance-table-per-subclass_xml.zip (9 KB)
Hibernate-inheritance-table-per-subclass_annotation.zip (9 KB)

Get our Articles via Email. Enter your email address.

You may also like...

34 Comments

  1. This is a very useful example and is presented clearly. Thanks you.

    One correction – the ER diagram at the beginning does not indicate the correct columns for table “owner”. It indicates the same columns as for the parent table “person”.

    • @Gerry – Oops, my mistake :) I will update the ER diagram. Thanks!

  2. gubs says:

    Hi,
    I followed the same and i get below exception

    Hibernate one to one (XML mapping)
    12/02/16 00:13:45 INFO common.Version: Hibernate Commons Annotations 3.2.0.Final
    12/02/16 00:13:45 INFO cfg.Environment: Hibernate 3.6.0.Final
    12/02/16 00:13:45 INFO cfg.Environment: hibernate.properties not found
    12/02/16 00:13:45 INFO cfg.Environment: Bytecode provider name : javassist
    12/02/16 00:13:45 INFO cfg.Environment: using JDK 1.4 java.sql.Timestamp handling
    12/02/16 00:13:45 INFO cfg.Configuration: configuring from resource: /hibernate.cfg.xml
    12/02/16 00:13:45 INFO cfg.Configuration: Configuration resource: /hibernate.cfg.xml
    12/02/16 00:13:45 WARN util.DTDEntityResolver: recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
    12/02/16 00:13:45 INFO cfg.Configuration: Reading mappings from resource : com/gubs/person.hbm.xml
    12/02/16 00:13:45 WARN util.DTDEntityResolver: recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
    12/02/16 00:13:45 INFO cfg.Configuration: Configured SessionFactory: null
    12/02/16 00:13:45 INFO cfg.HbmBinder: Mapping class: com.gubs.Person -&gt; person
    12/02/16 00:13:45 INFO cfg.HbmBinder: Mapping joined-subclass: com.gubs.Employee -&gt; employee
    12/02/16 00:13:45 INFO cfg.HbmBinder: Mapping joined-subclass: com.gubs.Owner -&gt; person
    12/02/16 00:13:45 ERROR hibernate.AssertionFailure: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
    org.hibernate.AssertionFailure: not a joined-subclass
    	at org.hibernate.mapping.Subclass.createForeignKey(Subclass.java:192)
    	at org.hibernate.cfg.HbmBinder.bindJoinedSubclass(HbmBinder.java:942)
    
    	at com.gubs.OneToOne.main(OneToOne.java:12)
    Initial SessionFactory creation failed.org.hibernate.AssertionFailure: not a joined-subclass
    Exception in thread "main" java.lang.ExceptionInInitializerError
    	at com.gubs.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
    	at com.gubs.HibernateUtil.(HibernateUtil.java:8)
    	at com.gubs.OneToOne.main(OneToOne.java:12)
    Caused by: org.hibernate.AssertionFailure: not a joined-subclass
    	at org.hibernate.mapping.Subclass.createForeignKey(Subclass.java:192)
    

    Can you help ?

    • gubs says:

      I got it. This issue is because i missed the extend method in the joined-subclass element. After adding it worked.

  3. alberlau says:

    @Inheritance(strategy=InheritanceType.JOINED)
    This type of inheritance can cause you problems because hibernate will join all tables in hierarchy. Oracle admin can kick your ass because of such queries. I’ve seen tens of tables joined resulting very large and slow queries in production environments.

  4. Alicia says:

    Hi,

    first of all, I have to thank you for Struts2 and Hibernate tutorials. They are really helpful for me.

    I’m doing a simple project similar to Struts2 and Hibernate example tutorial, but using Person and Owner. What I want to do is to add a new owner from a jsp, as you do in Struts2 and Hibernate example tutorial. I’m having some problems accesing to firstname or lastname properties.

    owner.jsp code:

    The action is called perfectly but it gives me this error when I try to add a new owner:
    Error setting expression ‘owner.firstname’ with value ‘[Ljava.lang.String;@1312237’
    ognl.OgnlException: target is null for setProperty(null, “firstname”, [Ljava.lang.String;@1312237)

    I don’t know what I’m doing wrong.

    Thanks in advance,

    Alicia.

  5. Alicia says:

    I finally found the error, it was related to properties’ getters and setters.

  6. jyoti says:

    Nice document for beginners,who are learning SPRING and Hibernate.

    Thanks,
    Jyoti

  7. Amar Panigrahy says:

    hi,
    Thanks for the wonderfully written hibernate tutorials.
    Also wanted to let u know in one table per subclass hierarchy tutorial..ur website page is showing the description of table per class hierarchy…but the code and mapping files are proper.

  8. andy says:

    It would be really nice if there was a print option so that I could print this out :-(

  9. Balamurugan says:

    Hi All,
    Are we using ‘discriminator’ in this example?
    Pls correct me, if I am wrong.

    Regards
    Bala S

  10. George says:

    Where is the discriminator column you speak of.

  11. George says:

    I have a question, what if (in your example layout) i have a person already stored in db and i want that person to be an owner or an employee, it was neither before, but i want it to be now.
    Im getting constraint errors when persisting or merging… how can i accomplish this scenario?

  12. Sid says:

    Discriminator tag is missing in the hbm file.
    Otherwise excellent tutorial!

  13. Pritam Banerjee says:

    Good tutorial !!

  14. safari says:

    how to map if “Employee” table has a composite primary key on it?

  15. Upendra Gupta says:

    Hi Viral
    I am a ECM Consultant and working on a applicaiton which is build on FileNet (ECM product) j2ee and hibernate.

    Appreciate your help in one of the issues i am facing. I am unable to save blob to database using hibernate.
    While commiting it throws error :

    Exception in thread “main” java.lang.AbstractMethodError:
    oracle.jdbc.driver.T2CPreparedStatement.setBinaryStream(ILjava/io/InputStream;)V

    I am using ojdbc14.jar and also tried with ojdbc6.jar , not sure what is going on

    Code is

    session = HibernateUtil.getAuomationSessionFactory().openSession();
    Transaction trans = session.beginTransaction();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead;
    stream = new FileInputStream(“C:\\Upendra\\Development\\ADL CITY\\Test.txt”);
    byte[] data1 = new byte[16384];
    data1 = IOUtils.toByteArray(stream);

    byte[] data = new byte[16384];

    while ((nRead = stream.read(data, 0, data.length)) != -1) {
    buffer.write(data, 0, nRead);
    }
    buffer.flush();
    Blob blob = session.getLobHelper().createBlob(buffer.toByteArray());
    emailattach.setFILENAME(“Test1.tif”);
    emailattach.setFILEBLOB(blob);
    session.save(emailattach);
    // session.flush();
    trans.commit();

  16. Swartblits says:

    Thanks very much for your attention to detail and understanding that when a person is researching something from scratch they need a simple good example. I went through a lot of pages before finding this clear concise explanation and example.
    Much much thanks.

  17. kalyani says:

    This is very useful and helped me to understand hibernate mappings and inheritance.

  18. arvin says:

    NEED To REMOVE::

    tag is used to define the discriminator column.

    tag is used to map the subclass Employee. Note that we have not used the usual tag to map Employee as it falls below in the hierarchy tree.

  19. srinivas says:

    Nice tutorials.

  20. Prateek says:

    nice tutorial,its really helpful .especially output database schema diagram.is easy to get the things.

  21. ivan says:

    Hi, Excellent tutorial. I’m new in Hibernate and it is very helpful. What happens when the owner is employee too? How to make that? Thanks

  22. Varun says:

    I implemented this approach, works fine as expected but I am having problems in fetching the record.
    So, when I do session.get() what kind of POJO object it would get ? the Parent one or child one ?
    Example:

    Vehicle.java
    
    public class Vehicle {
            private int vId;
            private String vname;
           // Getters and setters
    }
    
    Car.java
    
    public class Car extends Vehicle {
              private String model;
              // getter and setter
    }
    


    Implementing this as Table per sub class inheritance, two tables Vehicle and Car resp.
    Doing a save on vehicle object will save in Vehicle table and saving car will save in Vehicle and Car table.

    But now how to fetch each respectively ??

  23. Ankit Patel says:

    Hi Viral,

    I am using InheritanceType.Joined in one of my project.

    But when i am trying to update the data using HQL query it gives error that

    could not insert/select ids for bulk update
    org.hibernate.exception.SQLGrammarException: could not insert/select ids for bulk update
    	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
    	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    	at org.hibernate.hql.ast.exec.MultiTableUpdateExecutor.execute(MultiTableUpdateExecutor.java:151)
    	at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:421)
    	at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:283)
    	at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1288)
    	at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:117)
    	at com.j2.sepa.dao.DirectDebitDaoImpl.updateByMandateID(DirectDebitDaoImpl.java:55)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    Caused by: java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist
    
    	at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:440)
    	at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
    	at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:837)
    	at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:445)
    	at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:191)
    	at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:523)
    	at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:207)
    	at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1010)
    	at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1315)
    	at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3576)
    	at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3657)
    	at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1350)
    	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    	at org.hibernate.hql.ast.exec.MultiTableUpdateExecutor.execute(MultiTableUpdateExecutor.java:142)
    	... 48 more
     

    • Ankit Patel says:

      Sorry, In my previous comment i forgot to put the query generated by hibernate.

      Hibernate: /* insert-select for com.test.entity.B ids */ 
      		insert into HT_B select b.AKEY as AKEY 
      				from TEST.B b inner join TEST.A a on b.AKEY=a.AKEY where name123=?

      Below i give the sample program whih i used in my project

      Class A

      @Entity
      @Table(name = &quot;A&quot;, schema=&quot;TEST&quot;)
      @Inheritance(strategy = InheritanceType.JOINED)
      @DiscriminatorColumn(name = &quot;ATYPEKEY&quot;, discriminatorType = DiscriminatorType.INTEGER, length = 1)
      public abstract class A	{
      	
      	@Id
          @Column(name = &quot;AKEY&quot;, nullable = false)
          @GeneratedValue(strategy=GenerationType.SEQUENCE, generator=&quot;A_SEQ&quot;)
          @SequenceGenerator(name=&quot;A_SEQ&quot;, sequenceName=&quot;TEST.SEQ1_A&quot;, allocationSize=1)
          private Integer aKey;
      	
      	@Column(name=&quot;NAME&quot;)
      	private String name;
      	
      	@Column(name = &quot;XYZ&quot;)
      	private String xyz;
      	
      	|
      	|
      	|
      	etc.
      	
      }
      

      Class B

      @Entity
      @Table(name = &quot;B&quot;, schema=&quot;TEST&quot;)
      @DiscriminatorValue(value = &quot;3&quot;)
      public class B extends A{
      	
      	@Column(name = &quot;NAME123&quot;)
      	private String name123;
      	
      	@Column(name = &quot;XYZ123&quot;)
      	private String xyz123;
      	
      	@JoinColumn(name = &quot;CKEY&quot;, referencedColumnName = &quot;CKEY&quot;)
          @ManyToOne
      	private C c;
      
      	|
      	|
      	|
      	etc.
      	
      }						
      

      Class C

      @Entity
      @Table(name = &quot;C&quot;, schema=&quot;TEST&quot;)
      public class C {
      	
      	@Id
          @Column(name = &quot;CKEY&quot;, nullable = false)
          @GeneratedValue(strategy=GenerationType.SEQUENCE, generator=&quot;C_SEQ&quot;)
          @SequenceGenerator(name=&quot;C_SEQ&quot;, sequenceName=&quot;TEST.SEQ1_C&quot;, allocationSize=1)
          private Integer cKey;
      	
      	@Column(name = &quot;OPQ&quot;)
      	private String opq;
      	
      	|
      	|
      	|
      	etc.
      	
      }
      

      For updating Class B value i used below query.

      String hql = &quot;update B t0 set t0.c = (select t1 from C t1 where t1.opq = 'ANY') &quot; +
      			 &quot;where t0.name123 =:name123&quot;;
      			
      			Query query = getSession().createQuery(hql);
      			query.setString(&quot;name123&quot;, &quot;AnyThing&quot;);
      			return query.executeUpdate();
      

      • gaurav says:

        I dint think that for JOINED strategy we need to put any descriminator column.

  24. Daniel Eng says:

    Ok – i like this approach, but i have the following question.
    The information about a child can be directly removed from the database, thus causing us to have parents with no child – could this be an issue in the design?

    In your example – that means having persons who is not an employee or owner.

    • Jason C. Schmidt says:

      Are you aware of something called: “Cascade.ALL” ?

  25. Sandeep says:

    Hi Viral,

    Thanks for sharing the concept about Inheritance. I have a doubt. PFB my query:

    Suppose If I want to add primary key in the table of child class then how I can add?

    Thanks,
    Sandeep

    • gaurav says:

      It is not at all required to add any primary key in the child table if we have in parent one, because it will mapped to the parent table PK as we mentioned in the model as putting the @PrimaryKeyJoinColumn(name=”PERSON_ID”)

  26. Shashikanth Channagiri says:

    You need to fix this DISCRIMINATOR

  27. Dileep says:

    Hi,
    My concern is how does private fields of super class inherit to sub class. I am not getting this plz anyone help me.

  28. Amar says:

    In this tutorial , you have mentioned about Discriminator column. I think, this is wrong.
    The same doesn’t appear in mapping file (Person.hbm.xml) & tables too. which is quite obvious.

    I think,you have wrongly pasted this from previous seeion: table per hierarchy.
    Please look-into this

Leave a Reply

Your email address will not be published. Required fields are marked *