Search code examples
javahibernatejpasequences

What is the proper JPA mapping for @Id in parent and unique sequence in base classes


I have a class hierarchy:

abstract DomainObject {
...
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="SEQ")
    @SequenceGenerator(name="SEQ",sequenceName="SEQ_DB_NAME")
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;
...
}

BaseClass extends DomainObject {
...
   // Fill in blank here where this class's @Id will use a unique sequence generator
   // bonus points for any sort of automatic assignment of generator names that might 
   //prevent me from having to instrument all my domain objects uniquely
...
}

notes:

  • I do not specifically need a base class generator, so if it behooves me to remove it no problem.
  • This is an oracle 9i db if that is applicable
  • Hibernate 3.4 JPA
  • Spring 2.5 is available as well

Thanks


Solution

  • Okay here's how I ended up solving the problem:

    Base class:

    @MappedSuperclass
    public abstract class DomainObject implements Serializable {
     @Id
     @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="SEQ")
     @Column(name = "id", updatable = false, nullable = false)
     private Long id;
    
     .. rest of class
    }
    

    Descendant class:

    @Entity
    @SequenceGenerator(name="SEQ",sequenceName="SEQ_DB_NAME")
    public class BusinessObject extends DomainObject {
    
     ...
    
    }