Specifying distinct sequence per table in Hibernate on subclasses

Posted by gutch on Stack Overflow See other posts from Stack Overflow or by gutch
Published on 2010-12-30T07:25:55Z Indexed on 2010/12/30 8:54 UTC
Read the original article Hit count: 326

Filed under:
|
|
|

Is there a way to specify distinct sequences for each table in Hibernate, if the ID is defined on a mapped superclass?

All entities in our application extend a superclass called DataObject like this:

@MappedSuperclass
public abstract class DataObject implements Serializable {
    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id")
    private int id;
}

@Entity
@Table(name = "entity_a")
public class EntityA extends DataObject { ... }

@Entity
@Table(name = "entity_b")
public class EntityB extends DataObject { ... }

This causes all entities to use a shared sequence, the default hibernate_sequence.

What I would like to do is use a separate sequence for each entity, for example entity_a_sequence and entity_b_sequence in the example above. If the ID were specified on the subclasses then I could use the @SequenceGenerator annotation to specify a sequence for each entity, but in this case the ID is on the superclass. Given that ID is in the superclass, is there a way I can use a separate sequence for each entity — and if so, how?

(We are using PostgreSQL 8.3, in case that's relevant)

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate