Generic sequence generator for creating identifiers and database schemas using hbm2ddl

all. I have a problem with generating a DB schema via hbm2ddl. I want to use a common sequence generator for all private keys. Therefore, 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 identifiers.

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 the 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 this a problem, or am I doing something wrong?

+5
source share
2 answers

The solution to this problem was to define a common @SequenceGenerator in the package-ingo.java file for the package where my objects were located.

+2
source

, .. @SequenceGenerator, . :

@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;     
  ....
}
0

All Articles