logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Man-Chi Leung <manchi.leung+n...@gmail.com>
Subject what is the best way to validate unique properties before persistence (e.g. user email)
Date Wed, 15 Nov 2006 16:44:59 GMT

in appfuse's User schema: email  column is declared as unique in 
xdoclet-hibernate tag. therefore, no repeated email is allowed, or else 
hibernate will throw ConstraintViolationException, then Spring will 
rewrap it as DataIntegrityViolationException.

I wrote a test case as the following and it is passed successfully by 
catching Spring's DataIntegrityViolationException.

My question: what is the best pracitice to handle this?

1) AJAX Level: very likely, I am going to do a validation manager via 
DWR, when user keys in the email text field, there is a response on 
"repeated email error" instantly.

2) Action Level:  to be safe, i will validate again  at action level 
before passing to service layer.

3) DAO Level: one more validation for repeated email before finally 
calling dao.saveUser(user2);

yes, it is  too much. how can I make my life easier? any shortcut that 
I missed out ?

.... I am puzzled on how others are doing for this. PLS help! (sorry 
for my ignorance)

thanks to appfuse community

	public void testUpdateUserWithRepeatedEmail() throws Exception {
		User user1 = dao.getUser(new Long(1));
		User user2 = dao.getUser(new Long(2));
		try {
			fail("saveUser didn't throw DataIntegrityViolationException when 
same email is inserted");
		} catch (DataIntegrityViolationException e) {
			log.debug("expected exception: " + e.getMessage());

	public void saveUser(final User user) {
		if (log.isDebugEnabled()) {
			log.debug("user's id: " + user.getId());

===Exception Log:===
  [junit] [platform] ERROR [main] 
JDBCExceptionReporter.logExceptions(72) | Duplicate entry 
'matt_raible@yahoo.com' for key 3
    [junit] [platform] ERROR [main] 
AbstractFlushingEventListener.performExecutions(301) | Could not 
synchronize database state with session
    [junit] org.hibernate.exception.ConstraintViolationException: could 
not update: [com.rosonix.platform.model.User#2]

To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org

View raw message