views:

1515

answers:

1

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.

+4  A: 

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.

Vincent Ramdhanie
Link appears to be broken.For JBoss documentation: http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e1168
rpr