[jboss-user] [JBoss Seam] - Re: validation / exception handling patterns with EntityHome
mbertelsen
do-not-reply at jboss.com
Tue Jul 3 11:53:04 EDT 2007
Is it possible to implement a hibernate validator with this functionality (run a query to determine uniqueness)? It's not clear from looking at the hibernate validator docs how i would inject a persistence context to use.
BTW, I got the above strategy to work, but had to change a few things to get it to validate the unique property field itself. It wasn't clear how to set up the view to validate the *entire* object.
| @Name("clusterConfigUniqueValidator")
| @Validator
| @Transactional
| public class ClusterConfigUniqueNameValidator implements javax.faces.validator.Validator, Serializable{
| @In
| EntityManager entityManager;
|
| @In EntityHome<ClusterConfig> clusterConfigHome;
|
| public void validate(FacesContext facesContext, UIComponent component,
| Object value) throws ValidatorException {
| String name = (String) value;
| ClusterConfig cc = clusterConfigHome.getInstance();
| try {
| ClusterConfig cc1 = (ClusterConfig) entityManager.createQuery("select cc from ClusterConfig cc where cc.name = :name")
| .setParameter("name", name).getSingleResult();
| if (cc1 != null && !cc1.getId().equals(cc.getId())) {
| throw new ValidatorException(new FacesMessage("Name must be unique"));
| }
| } catch (NoResultException nre) {
| // that's fine - this name is unique.
| }
| }
|
| }
|
| And then on the edit page:
|
| <s:decorate id="nameDecoration" template="layout/textField.xhtml">
| <ui:define name="label">Name:</ui:define>
| <h:inputText fieldIdBase="name" id="name" value="#{clusterConfig.name}" required="true">
| <f:validator validatorId="clusterConfigUniqueValidator"/>
| <s:validate/>
| </h:inputText>
| </s:decorate>
|
|
Also, what about dealing with SQLException when you delete something and violate a foreign key constraint? Does anybody have a recommendation for how to avoid this gracefully? Should I not display the delete link/button on my list page if it's referenced (which would require an extra query per entity listed)? Write a custom exception handler in pages.xml to catch SQLException and a custom error page/handler that somehow parses the message?
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4060043#4060043
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4060043
More information about the jboss-user
mailing list