-
1. Re: Exception for duplication
thelith85 May 24, 2008 7:59 PM (in response to thelith85)And most of the time it gives me this error page
JBoss Seam Debug Page
This page allows you to view and inspect any component in any Seam context associated with the current session.
Conversations
.
.
.
. -
-
3. Re: Exception for duplication
fernando_jmt May 26, 2008 7:42 PM (in response to thelith85)have you tried putting something like this in pages.xml:
<exception class="javax.persistence.EntityExistsException"> <end-conversation/> <redirect view-id="/errorPage.xhtml"> <message severity="error">Item already exists</message> </redirect> </exception>
In the other hand you can also catch the exception (javax.persistence.EntityExistsException) in your bean class.
HTH.
-
4. Re: Exception for duplication
stephen May 26, 2008 9:08 PM (in response to thelith85)Catching exceptions from the persistence layer really is a last resort.
You should additionally check with a JSF validator. For example here's a validator that checks if a user id is already taken (which is quite similar to your scenario):/** * JSF validator that checks if a user name is already taken by another user. * The DB itself has a unique constraints on the user name column, so it is sufficient to check this on GUI level. */ @Name("userIdUniqueValidator") @Validator @Transactional public class UserIdUniqueValidator implements javax.faces.validator.Validator, Serializable { @In private EntityManager entityManager; @In(required = false) private User selectedUser; public UserIdUniqueValidator() { } public void validate(FacesContext facesContext, UIComponent component, Object value) throws ValidatorException { try { Long id = (Long) entityManager.createQuery("select user.id from User user where user.userId = :userId").setParameter("userId", value).getSingleResult(); if (selectedUser != null && !id.equals(selectedUser.getId())) { throw new ValidatorException(new FacesMessage("User Id is already in use. Please choose a different one.")); } } catch (NoResultException nre) { // fine - userName is unique. } } }
You can use that validator (nested in another component tag) like
<f:validator validatorId="userIdUniqueValidator"/>
Of course you scenario is a little more tricky, because it involves two fields (person and course). You'll have to attach the validator to the seconds component and retrieve the first components submitted value from the component tree (assuming you have two fields for person and course).
See here for more information
Blog