I am facing an issue with hibernate generated identifier. I am using Sequence Generator to generate my identifier. Database is Oracle 11g and hibernate version 3.6.1.
I am aware that the default allocationSize in hibernate is 50 and hence hibernate makes calls to database every 50 inserts to fetch the identifier. Hence I am changing the allocationSize to 1000 to reduce database calls as below:
@GeneratedValue(generator = "test_gen", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "test_gen", sequenceName = "myschema.my_oracle_sequence", allocationSize = 1000)
There are few issues though.
First, this is an enhancement, hence I cant upgrade the hibernate version to make use of the new sequence generators.
Second, I cant use the below property as this will impact other hibernate entities, which should not be touched by my change.Is there some way to tell hibernate to apply the below property to only one entity?
hibernate.id.new_generator_mappings=true
Third, I tried the below hibernate specific mapping. However, I am required to provide an initial value here. If not, it takes initial value as 1 and computes the identifier and I get as expected a Constraint Exception as the identifier has already been used. I cant provide an initial value here again as the code is already in use and both primary id and nextval of the sequence have moved from their initial values.
@GenericGenerator(
name = "sequenceGenerator",
strategy = "enhanced-sequence",
parameters = {
@org.hibernate.annotations.Parameter(
name = "optimizer",
value = "pooled"
),
@org.hibernate.annotations.Parameter(
name = "increment_size",
value = "500"
)
}
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "sequenceGenerator"
)
Fourth, I cant use the code that I have mentioned in first code block, as I randomly see UniqueConstraintViolationException when inserting the records as has been seen by few other folks. JPA 2 @SequenceGenerator @GeneratedValue producing unique constraint violation
With all these constraints, is there any way I can increase the allocationSize so as to improve the records insertion performance. I would inserting around 10-40mn records in a single batch.
Regards, V