In Hibernate, the mapping strategy you used is known as "Table per concrete class with unions". What you probably want to do is "Table per concrete class with implicit polymorphism". Refer to "Java Persistence with Hibernate" for further information, but basically, you have to remove the annotations @Inheritance and add this annotation to your class: @MappedSuperclass . The subclasses won't have any additional mapping, other than @Entity.
Note that "@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)" in Hibernate shares the ID between the implementations of the parent class (for the union): "The database identifier and its mapping have to be present in the superclass, to be shared across all subclasses and their tables. "
It would be something like this (note that I tested with MySQL 5.1 and Hibernate Core 3.2.6.ga with Hibernate Annotations 3.3.1.GA):
Order:
Code:
@Entity
@MappedSuperclass
public abstract class Order {
@Id
private Long id;
@Column
private String name;
...
}
OrderA:
Code:
@Entity
public class OrderA extends Order {
@Column
private String nameOrderA;
...
}
OrderB:
Code:
@Entity
public class OrderB extends Order {
@Column
private String nameOrderB;
...
}
Service:
Code:
Session s = getSession();
Transaction tx = s.beginTransaction();
OrderA orderA = new OrderA();
orderA.setId(1L);
orderA.setName("Name of the order A");
orderA.setNameOrderA("Name of the order A");
OrderB orderB = new OrderB();
orderB.setId(1L);
orderB.setName("Name of the order B");
orderB.setNameOrderB("Name of the order B");
s.saveOrUpdate(orderA);
s.saveOrUpdate(orderB);
tx.commit();
s.close();