I have an unusual situation which requires that I obtain unique identifiers from an Oracle sequence without actually saving a row to the table which relies on the sequence.
My entity:
@Table(name = 'SUBMISSION')
@Entity(name = 'Submission')
class Submission {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = 'submissionSeq')
@SequenceGenerator(
name = 'submissionSeq',
sequenceName = 'SUBMISSION_SEQ',
allocationSize = 50
)
@Column(name = 'SUBMISSION_ID', nullable = false)
Long submissionId
...
My repository:
@Repository
interface SubmissionRepository extends JpaRepository<Submission, Long> {
@Query(value = 'SELECT submission_seq.NEXTVAL FROM DUAL', nativeQuery = true)
Long getNextSequenceValue()
}
As you can see, the identifier for the SUBMISSION table is generated from an Oracle sequence, whose allocation size is set to 50. The JPA provider (Hibernate) is caching those IDs in memory to avoid a call to the database to obtain a sequence value each time a Submission record is persisted.
To get a single value from this sequence, I am using a Spring Data native query. However, because the sequence is set to increment by 50, this wastes 49 unique IDs each time this native query is invoked.
Is there a way to obtain a submissionId from the cached values stored within Hibernate's pool of allocated IDs instead of wasting IDs?
SequenceStyleGeneratorIf you dont want to use hibernate sequence generator then I guess other way is manually fetch Ids from the sequence by creating a separate sequence with increment by 1SequenceStyleGeneratordelegates the generation to its Optimizer for which there is apublic Optimizer getOptimizer(), the problem is to get your hands on the generator used... highly depends on the Hibernate version used.