[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - Can't catch exception caused by unique constraint?

Dellerium do-not-reply at jboss.com
Sat Feb 28 13:47:58 EST 2009


Hi forum!

I'm trying to write a small application for testing and learning...

I've created an entity (useraccount) with a unique constraint on firstname. To test it, I wrote a small junit test which gets the reference via jndi lookup. Insert a new useraccount is no problem. So I tried to test the unique constraint. As it should, I get an exception if I try to persists a second, identical useraccount.

But I'm not able to catch the exception.... I thought, that it would be a good idea, to throw some exeptions when for example an identical useraccount should be persist, but atm I don't know, how get this running...

I also found this post: http://www.jboss.org/index.html?module=bb&op=viewtopic&t=71611&start=0&postdays=postDays&postorder=postOrder&highlight=highlight but I didn't figure out, what I've done wrong...


Jboss 5.0.0.GA, ejb3

best regards,
Andre


Exception in serverlog:

  | 19:11:47,775 WARN  [JDBCExceptionReporter] SQL Error: 0, SQLState: null
  | 19:11:47,776 ERROR [JDBCExceptionReporter] Batch-Eintrag 0 insert into t_useraccount (version, disabled, personOfUserAccount, userlogin, userPassword, id) values (NULL, 0, NULL, foo, bar, 2) wurde abgebrochen.  Rufen Sie 'getNextException' auf, um die Ursache zu erfahren.
  | 19:11:47,776 WARN  [JDBCExceptionReporter] SQL Error: 0, SQLState: 23505
  | 19:11:47,776 ERROR [JDBCExceptionReporter] FEHLER: doppelter Schluesselwert verletzt Unique-Constraint »t_useraccount_userlogin_key«
  | 19:11:47,776 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
  | org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
  | 	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
  | 	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
  | 	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
  | 	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
  | 	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
  | 
  | [.......]
  | 


My SLSB:




  | 	public Integer saveUserAccount(UserAccount userAccount) throws UserAccountAlreadyExistException {
  | 		
  | 		try {
  | 			em.persist(userAccount);
  | 			em.flush();
  | 			return userAccount.getId();
  | 		}
  | 		catch (EntityExistsException eee) {
  | 			throw new UserAccountAlreadyExistException();
  | 		}
  | 		catch (ConstraintViolationException e) {
  | 			throw new UserAccountAlreadyExistException();
  | 		}
  | 
  | 	}
  | 

UserAccountAlreadyExistException:

  | @ApplicationException(rollback=true)
  | public class UserAccountAlreadyExistException extends Exception {
  | 
  | 	private static final long serialVersionUID = 1L;
  | 
  | 	public UserAccountAlreadyExistException() {
  | 		super();
  | 	}
  | 
  | 	public UserAccountAlreadyExistException(String s) {
  | 		super(s);
  | 	}
  | }
  | 





View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4213959#4213959

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4213959




More information about the jboss-user mailing list