Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared sequence generator for IDs and DB scheme creation using hbm2ddl

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 ?

like image 847
Vladimir Avatar asked Oct 07 '11 14:10

Vladimir


People also ask

How would you implement a custom sequence based id generator?

If you want to use a custom generator, you need to define the generator in a @GenericGenerator annotation and provide the fully-qualified classname as the strategy. You can also configure a set of parameters that will be provided to the configure method when Hibernate instantiates the generator.

What is allocationSize in sequence generator in Hibernate?

allocationSize. (Optional) The amount to increment by when allocating sequence numbers from the sequence.

What is @SequenceGenerator in Hibernate?

With SequenceGenerator Hibernate will query the database only when amount of IDs specified by allocationsize runs out. If you set up allocationSize = 1 then it's the reason why Hibernate query the DB for each insert. Change this value, and you are done.

What is @GeneratedValue strategy GenerationType identity?

The @GeneratedValue annotation is to configure the way of increment of the specified column(field). For example when using Mysql , you may specify auto_increment in the definition of table to make it self-incremental, and then use @GeneratedValue(strategy = GenerationType.IDENTITY)


1 Answers

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

like image 97
Vladimir Avatar answered Nov 15 '22 08:11

Vladimir