6

all. I have an issue with DB scheme generation via hbm2ddl. I want to use shared sequence generator for all private keys. So I defined it once in some entity.

@Entity
@SequenceGenerator(name = "MY_SEQUENCE_GENERATOR", sequenceName = "MY_SEQ")
public class MyEntity implements Serializable {
 ....
}

Then I want to use this sequence generator for all ids.

public class SomeEntity1 implements Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator =  "MY_SEQUENCE_GENERATOR")
  Long id;     
  ....
}

public class SomeEntity2 implements Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator =  "MY_SEQUENCE_GENERATOR")
  Long id;     
  ....
}

When I run hbm2ddl ant task I get an exception:

[hibernatetool] javax.persistence.PersistenceException: org.hibernate.AnnotationException: Unknown Id.generator: MY_SEQUENCE_GENERATOR
[hibernatetool] org.hibernate.AnnotationException: Unknown Id.generator: MY_SEQUENCE_GENERATOR

Is it an issue or I'm doing something wrong ?

Vladimir
  • 163
  • 1
  • 1
  • 8

2 Answers2

2

The solution of this porblem was defining shared @SequenceGenerator in package-ingo.java file for package were my entities placed.

Vladimir
  • 163
  • 1
  • 1
  • 8
0

The sequence import, i.e. @SequenceGenerator annotation should be presented in all classes. So make an abstract class with this annotation and extend all entities from it:

@SequenceGenerator(name = "MY_SEQUENCE_GENERATOR", sequenceName = "MY_SEQ")
public abstract class BaseEntity implements Serializable {
 ....
}

public class SomeEntity1 extends BaseEntity {
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator =  "MY_SEQUENCE_GENERATOR")
  Long id;     
  ....
}

public class SomeEntity2 extends BaseEntity {
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator =  "MY_SEQUENCE_GENERATOR")
  Long id;     
  ....
}
forker
  • 2,609
  • 4
  • 23
  • 24
  • Thanks for response @forker. But javadoc for @SequenceGenerator says us that `"A sequence generator may be specified on the entity class or on the primary key field or property. The scope of the generator name is global to the persistence unit (across all generator types)"`. – Vladimir Oct 10 '11 at 07:48