Java/Hibernate JPA: InheritanceType.TABLE_PER_CLASS and IDs
Asked Answered
E

1

4

I'm using Hibernate JPA.

Suppose I have these classes:

AbstractPerson
|--> ConcreteEmployee
|--> ConcreteCustomer

Is there any way to make the concrete classes have independent IDs?

I'm using InheritanceType.TABLE_PER_CLASS.

Estabrook answered 1/7, 2010 at 1:57 Comment(2)
In at least one of the concrete classes, I'd like to have autoincremental numbers, like a customer number, independent of the 'shared' ID. This is for a university assignment, so opinions on why this could be a bad idea are welcome. :PEstabrook
It is possible with the Mapped superclass inheritance.Lifeblood
A
11

From the Hibernate Annotations Reference Guide:

2.2.4.1. Table per class

This strategy has many drawbacks (esp. with polymorphic queries and associations) explained in the JPA spec, the Hibernate reference documentation, Hibernate in Action, and many other places. Hibernate work around most of them implementing this strategy using SQL UNION queries. It is commonly used for the top level of an inheritance hierarchy:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Flight implements Serializable { ... } 

This strategy supports one-to-many associations provided that they are bidirectional. This strategy does not support the IDENTITY generator strategy: the id has to be shared across several tables. Consequently, when using this strategy, you should not use AUTO nor IDENTITY.

So I'm afraid what you want is not supported (and I suggest to use GenerationType.TABLE).

Artieartifact answered 1/7, 2010 at 17:39 Comment(1)
Ah, thanks. I'll have to generate the customer number myself then.Estabrook

© 2022 - 2024 — McMap. All rights reserved.