Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MappedSuperclass - Change SequenceGenerator in Subclass

I'm using JPA2 with Hibernate and try to introduce a common base class for my entities. So far it looks like that:

@MappedSuperclass public abstract class BaseEntity {      @Id     private Long id;      @Override     public int hashCode() {         // ...     }      @Override     public boolean equals(Object obj) {         // ...     }      public Long getId() {         return this.id;     }      public void setId(Long id) {         this.id = id;     } } 

However, for every table theres a sequence $entityname_seq which I want to use as my sequence generator. How can I set that from my subclass? I think I need to override @GeneratedValue and create a new SequenceGenerator with @SequenceGenerator.

like image 817
atamanroman Avatar asked Dec 21 '11 12:12

atamanroman


2 Answers

Yes, it is possible. You can override the default generator name with the @SequenceGenerator annotation.

  • Base class
    @MappedSuperclass     public abstract class PersistentEntity implements Serializable     {         private static final long serialVersionUID = 1L;          @Id         @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "default_gen")         protected Long id = 0L;          public Long getId()         {             return id;         }          public void setId(Long id)         {              this.id = id;         }     }
  • Sequence (SQL)

    create sequence role_seq;
  • Derived class

    @Entity     @Table(name = "role")     @SequenceGenerator(name = "default_gen", sequenceName = "role_seq", allocationSize = 1)     public class Role extends PersistentEntity implements Serializable     {         private static final long serialVersionUID = 1L;          @NotNull         @Size(max = 32)         private String name;          public String getName()         {              return name;         }          public void setName(String name)         {              this.name = name;         }        }
  • This approach worked fine in Hibernate 4.1.x, but it didn't in EclipseLink 2.x.

edit

  • As per the comment, it seems to be working with EclipseLink 2.6.1-RC1.
like image 83
rbento Avatar answered Sep 23 '22 19:09

rbento


In JPA that cannot be done with annotations. Annotation itself cannot be overridden. Entity inherits all the mapping information from MappedSuperClass. There is only two annotations that can be used to redefine mappings inherited from mapped superClass:

  1. AttributeOverride to override column mappings and
  2. AssociationOverride to override join columns / table.

Neither of them helps with GeneratedValue.

like image 35
Mikko Maunu Avatar answered Sep 23 '22 19:09

Mikko Maunu