Issue with sequence why two entity are sharing the same sequence when generating schema with hbm2ddl ?
You are using the Global sequence generator that hibernate provide by default when no generator is provided as specifed by the JPA Spec. To have a private generator you should declare a private generator with the annotation @SequenceGenerator
and set the generator
attribute of the @GeneratedValue
annotation
Extracted from javadoc
@GeneratedValue
(Optional) The name of the primary key generator to use as specified in the SequenceGenerator or TableGenerator annotation.Defaults to the id generator supplied by persistence provider.
SequenceGenerator
This annotation defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation. 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).Example:
@SequenceGenerator(name="EMP_SEQ", sequenceName="private_sequence")
Hibernate recommends that new projects use hibernate.id.new_generator_mappings=true as the new generators are more efficient and closer to the JPA 2 specification semantic
Section 1.3. Properties
2.2.3. Mapping identifier properties
Complete example
@Entity
@SequenceGenerator(name="PRIVATE_SEQ", sequenceName="private_sequence")
public class test {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="PRIVATE_SEQ")
Long id;
}