How to persist Inherited third party classes

Hello,

I have a third party class for which I don’t have access to it’s source code. I have inherited that class into my class.

How can I

  1. Persist Parent class to individual table.
  2. Persist Child class along with Parent class data in the single table.

Assume I have third party class “Entity”

public class Event {
	private Long id;

	private String title;
	private Date date;
// Getters and Setters.
}

And a child class extended from “Entity” class.

public class ChildEntity extends Entity {
         String Address;
         Integer phNo;
// Getters and Setters.
}

Thank you.

Hi, you can find examples of the different strategies for inheritance in this paragraph of the documentation: http://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#entity-inheritance

I think what you are looking for is the table per class strategy:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Entity {

}

@Entity
public Child extends Entity {

}

I will let you check the documentation for additional details.

Hello Davide,

The only thing is, I don’t have access to super class to annotate that class for Table per class or any annotation.

So how to persist “third party” super class for which I don’t have access and other scenario in the Original post?

Thank you.

You have to define the mapping using XML.

You can find a detailed explanation of the XML files in Chapter 12 of the JPA JSR documentation.

@padmahasa TABLE_PER_CLASS does not perform very well, so you might want to consider that decision. JOINED inheritance is a much better alternative if you have many subclasses.

The SINGLE_TABLE is also a good alternative since you only need to add columns, not create new tables.