-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate HQL causing SQL Error 1062 (Duplicate Entry)
PostPosted: Tue Jul 17, 2012 5:51 pm 
Newbie

Joined: Tue Jul 17, 2012 5:19 pm
Posts: 2
Hoping you guys can help me debug this thing.

My Function:
Code:
public boolean getEmployeeExists(String email) {
   Query q = hsm.getSession().createQuery("select count(e) from Employee e where e.email = :email");
   q.setString("email", email);

   return (Long) q.uniqueResult() > 0;
}


I know the return is sortof a hacky way to return, but this is not my code. It never actually returns anyway:

SQL Error: 1062, SQLState: 23000
Duplicate entry 'test@test.com' for key 'email'

A test@test.com email is already in the database as an employee email, so this function SHOULD return true, but it doesn't.. really cannot figure this out... Ugh.


Top
 Profile  
 
 Post subject: Re: Hibernate HQL causing SQL Error 1062 (Duplicate Entry)
PostPosted: Tue Jul 17, 2012 7:16 pm 
Newbie

Joined: Tue Jul 17, 2012 5:19 pm
Posts: 2
I found the problem... and it's nowhere in this code. It's a pretty silly situation really. I was using Hibernate in Conjunction with Apache-Tapestry5 and have now learned that Tapestry5 will modify a Hibernate object even before passing through the "validate" event. Trying to have the Hibernate session perform any queries at all will make Hibernate try to commit all database-persistant objects before executing the query (from what I can tell) and if there is a failure with the database requirements (as there was in this case) it will cause an exception based on that. This is particularly un-useful behavior in the given case, where validation was specifically trying to be performed.

The way around this of course is to search through the persistant entities (in this case Employee) and compare based on email, but that only works if you have already got them around and don't have to hit the Hibernate-abase for them.


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