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 ?
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.
allocationSize. (Optional) The amount to increment by when allocating sequence numbers from the sequence.
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.
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)
The solution of this porblem was defining shared @SequenceGenerator in package-ingo.java file for package were my entities placed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With