-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Using @Id @GeneratedValue causes Unique Constraint violated
PostPosted: Tue Mar 11, 2008 11:42 am 
Newbie

Joined: Tue Mar 11, 2008 11:28 am
Posts: 3
Hi,

I am trying to get the hibernate annotations working for my existing code. I have installed hibernate annotations libraries as mentioned in the tutorial and it was all working fine, but... when I try to create an entity in the database, I am getting the Unique Constraint Violated error.

I am using @Id @GeneratedValue(strategy=GenerationType.AUTO) for my primary key which is an Integer.

Is there any way to tell the @Id or @GeneratedValue annotation to get the max of the column in the database and start sequencing from that value?

The table where I am trying to insert the data already have 4 rows in it, I have implemented this annotation now for the entity and trying to insert data using this, where I am getting this error. I have tried with all the strategies, but still I am stuck with this.

Can anyone please throw some light in here?

Hibernate version: 3.2

Full stack trace of any exception that occurs:

WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1062, SQLState: 23000
ERROR org.hibernate.util.JDBCExceptionReporter - Duplicate entry '0' for key 1
org.hibernate.exception.ConstraintViolationException: could not insert

Name and version of the database you are using: MySql Server 5


Thanks,
Karikalan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 3:57 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
There think you could use the increment generator. Since this is not a supported JPA GenerationType I think you would have to map it like this:

Code:
...
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(generator = "generator")
long myId;
...


Haven't tried this though. Good luck.

--hardy


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 7:11 am 
Newbie

Joined: Tue Mar 11, 2008 11:28 am
Posts: 3
Thanks Hardy, it works.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.