-->
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.  [ 7 posts ] 
Author Message
 Post subject: Duplicate Entry Exception...
PostPosted: Mon Dec 03, 2007 7:56 am 
Newbie

Joined: Mon Dec 03, 2007 7:50 am
Posts: 3
Well...
I'm having a problem with duplicate entry...
I'm using hibernate annotation and mysql...

com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry 'd' for key 2

I've tryed to circunde it with try catch but doesnt work...
Someone can tell me how can i do it?

The idea is that a person try to register in a website... but the login he has choosed was already registered...
Login in "unique" in mysql...


Er... And sorry... I don't speak english that good :S


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 1:28 pm 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

I did it this way to maintaing unique result in database.

Criteria criteria = session.createCriteria(Login.class);
Login login = (Login) criteria.add(Restrictions.naturalId().set("loginId", loginId)).setCacheable(true).uniqueResult();

where Login Id is unique in this case.

Hope this helps:-)

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject: Hibernate Validator
PostPosted: Mon Dec 03, 2007 1:43 pm 
Newbie

Joined: Mon Dec 03, 2007 7:50 am
Posts: 3
but isn't there another way for this?

i mean... i've hearded about hibernate validator...
Someone can tell me how it works? i've saw in hibernate website.. but i dont understood... can i use it to validate the "login" or something that i need to have only once in database?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 1:51 pm 
Newbie

Joined: Mon Dec 03, 2007 7:50 am
Posts: 3
i want that my userlogin be only once in database...
how should i use this Criteria?

have u some tutorial of this??
i've tryed to use him here but doesnt work... im not mapping its good...

What i want to be unique its not my primary key... so its not a "naturalId"...
its the name of the user.. also the email...

is there someone to help me? ^^^


Thanks!! =D


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 2:11 pm 
Beginner
Beginner

Joined: Sat Oct 20, 2007 8:28 am
Posts: 28
i have the same problem with a class that i generate on a job with 1 minute interval and sometimes will generate a constraintviolatin and since every minute i open a new session i can't use what you suggested ramnath...
isn't there a pretier way to do this then throw a huge stacktrace ?
or somehow to filter the log4j not to log that exception ...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 04, 2007 2:57 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
@ ALL,

If it is the unique is not ur primary key, Look into the posting below..
http://forum.hibernate.org/viewtopic.ph ... highlight=

You would find the answer of gavin & others....

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 2:07 pm 
Beginner
Beginner

Joined: Sat Oct 20, 2007 8:28 am
Posts: 28
ramnath wrote:
@ ALL,

If it is the unique is not ur primary key, Look into the posting below..
http://forum.hibernate.org/viewtopic.ph ... highlight=

You would find the answer of gavin & others....


sorry to bump an old thread , but it's a follow up
what if it is my PK that is constraintviolated is there still a way to avoid the exception ? i can't use saveOrUpdate because as mentioned above it's on a schedueled job with 1 min interval .


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.