Return-Path: Delivered-To: apmail-logging-log4j-user-archive@www.apache.org Received: (qmail 13530 invoked from network); 15 Nov 2006 16:46:12 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 15 Nov 2006 16:46:12 -0000 Received: (qmail 32577 invoked by uid 500); 15 Nov 2006 16:46:19 -0000 Delivered-To: apmail-logging-log4j-user-archive@logging.apache.org Received: (qmail 32547 invoked by uid 500); 15 Nov 2006 16:46:19 -0000 Mailing-List: contact log4j-user-help@logging.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Log4J Users List" Reply-To: "Log4J Users List" Delivered-To: mailing list log4j-user@logging.apache.org Received: (qmail 32525 invoked by uid 99); 15 Nov 2006 16:46:19 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 15 Nov 2006 08:46:19 -0800 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (herse.apache.org: domain of jak-log4j-user@m.gmane.org designates 80.91.229.2 as permitted sender) Received: from [80.91.229.2] (HELO ciao.gmane.org) (80.91.229.2) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 15 Nov 2006 08:46:04 -0800 Received: from list by ciao.gmane.org with local (Exim 4.43) id 1GkNt3-0007H0-M3 for log4j-user@logging.apache.org; Wed, 15 Nov 2006 17:45:22 +0100 Received: from 220-133-60-83.hinet-ip.hinet.net ([220.133.60.83]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 15 Nov 2006 17:45:21 +0100 Received: from manchi.leung+news by 220-133-60-83.hinet-ip.hinet.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 15 Nov 2006 17:45:21 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: log4j-user@logging.apache.org From: Man-Chi Leung Subject: what is the best way to validate unique properties before persistence (e.g. user email) Date: Thu, 16 Nov 2006 00:44:59 +0800 Lines: 68 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: 220-133-60-83.hinet-ip.hinet.net User-Agent: Unison/1.7.7 Sender: news X-Virus-Checked: Checked by ClamAV on apache.org hi, 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 ~thinkboy ===UserDaoTest.java=== public void testUpdateUserWithRepeatedEmail() throws Exception { User user1 = dao.getUser(new Long(1)); User user2 = dao.getUser(new Long(2)); try { user2.setEmail(user1.getEmail()); dao.saveUser(user2); fail("saveUser didn't throw DataIntegrityViolationException when same email is inserted"); } catch (DataIntegrityViolationException e) { assertNotNull(e); log.debug("expected exception: " + e.getMessage()); } } ===UserHibernateDao.java=== public void saveUser(final User user) { if (log.isDebugEnabled()) { log.debug("user's id: " + user.getId()); } getHibernateTemplate().saveOrUpdate(user); getHibernateTemplate().flush(); } ===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