More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.
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.
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.