JPA Hibernate Sequence generator generating odd ID value and IntegrityConstraintVolation exception is coming

When you do not want only hibernate sequence to generate primary key of your all tables then you have three options given by JPA and Hibernate .

  1. Sequence Generator
  2. Identity Column
  3. Table Sequence

Let’s talk about Sequence generator and it’s proper use so that it won’t give you Odd and Negative Numbers as your primary key .

Sequence Generator : To use a specific named sequence object (whether it is generated by schema generation or already exists in the database) you must define a sequence generator using a @SequenceGenerator annotation. In this you provide a unique name as name tag, provide database sequence name in sequenceName tag.You can define sequencegenerator at top of class or field both. To use this sequenceGenerator you need to define @Generatedvalue tag with stratgeyType as sequence and sequenceGenerator name on field for which you want this value to be assigned.
Refer below code.


@Entity
public class Inventory implements Serializable {


@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="InvSeq")
@SequenceGenerator(name="InvSeq",sequenceName="INV_SEQ")
private long id;

We have learn how to use sequence generator but still there is possibility of Odd and Negative Numbers as primary key values.

It will happen when database sequence is already exist in database and you are not defining database through hibernate.
How it is happening: Well JPA have allocationSize tag in Sequencegenerator , value of allocationSize is 50 by default if you have not assigned any value. So when Jpa persist that record and demand sequence value from database, it treat that database sequence has incremented that value by 50 so it will substract 50 from that value and try to persist record with that value.
On the other hand database is incrementing value by it’s increment by syntax given at time of it’s defination . Which is not guaranteed to be increment by 50. So there is a possibility of value mismatch and odd value generation which could give you integrityconstraintviolation exception.
So to avoid this problem you need to define allocationSize equal to increment by of database sequence or as 1.Refer below code.

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="InvSeq")
@SequenceGenerator(name="InvSeq",sequenceName="INV_SEQ", allocationSize=1)
private long id;

If you find this post useful please follow,comment and share it.Please feel free to ask questions if you have any.

Other Related Posts :

Set datasource spring bean properties dynamically if server is not available
Logback logs are not working. Log4j logs are not working. Steps to make logging work.
MQ connection not closed and giving MQ Error-2009

Advertisements

6 thoughts on “JPA Hibernate Sequence generator generating odd ID value and IntegrityConstraintVolation exception is coming

  1. Pingback: String vs StringBuffer vs StringBuilder | Techno Tailor

  2. Pingback: String interview questions and answers | Techno Tailor

  3. Pingback: MQ connection not closed and giving MQ Error-2009 | Techno Tailor

  4. Pingback: Top 10 spring interview questions and their answers. Spring basic concepts. | Techno Tailor

  5. Pingback: How to render HTML in java | Techno Tailor

  6. Pingback: Set datasource spring bean properties dynamically if server is not available | Techno Tailor

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s