Archive

Archive for March, 2012

Using sequence, DB2 and Hibernate

March 4, 2012 Leave a comment

Several days ago, i found strange behavior  in DB2 sequence using HIbernate ORM framework.

i have created sequence named HIST_VND_DISTRICT_SEQ, with

initial value 100

minimal value=100

increment by 1

To initiate sequence in model, i use the following annotation :

@Id
@SequenceGenerator(name="HIST_VND_DISTRICT_SEQ",sequenceName="HIST_VND_DISTRICT_SEQ")
@GeneratedValue(generator="HIST_VND_DISTRICT_SEQ",strategy=GenerationType.SEQUENCE)
@Column(name = "VND_DISTRICT_ID", unique = true, nullable = false)

But, when i execute a method from web page front end, the result is unexpected, instead of generate proper value, it generated random value start from 5000.

I have been searching for help from dozen of hibernate forum but no luck.

then i try to read all documentation related to sequence in hibernate, and try several configuration and finnaly got an answer.

i just need to put allocationSize=1, then it works like charm.

the final annotation model should be like this :

@Id
@SequenceGenerator(name="HIST_VND_DISTRICT_SEQ",sequenceName="HIST_VND_DISTRICT_SEQ",allocationSize=1)
@GeneratedValue(generator="HIST_VND_DISTRICT_SEQ",strategy=GenerationType.SEQUENCE)
@Column(name = "VND_DISTRICT_ID", unique = true, nullable = false)

Bog camp, March 4th 2012

A. Ahmad Kusumah

Advertisements
Categories: Database, Java Tags: , ,