Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactoring a Hibernate entity into subclasses

I have a class that is currently mapped as an entity in a database table using Hibernate. This class should be refactored into an abstract class containing some field common to all of its subclasses.

I'm using annotations for mapping hibernate entities/relationships classes.

I would like suggestions/variants on how to do this refactoring.

Also, some suggestions on how to move the data that is stored in the database (for the future abstract superclass) into one of the concrete subclasses.

like image 481
Mattias Holmqvist Avatar asked Dec 02 '08 14:12

Mattias Holmqvist


1 Answers

First, I will create the superclass and add the necessary annotations. You have to decide between:

  • Table per Class Strategy
  • Single Table per Class Hierarchy Strategy
  • Joined Subclass Strategy

I think the Joined Subclass will work here. You add the annotation:

@Entity
@Inheritance(strategy=InheritanceType.JOINED)

To the super class.

Second, I will create the table(s) that represent the sub classes. Remember these will only have the columns that are unique to the subclass, columns that are shared will remain in the super class. Then select the rows from the super class' table that belong in each subclass and move the data.

I'm not sure if you are looking for something more specific? This article explains inheritance with Hibernate.

like image 131
Vincent Ramdhanie Avatar answered Oct 16 '22 22:10

Vincent Ramdhanie