Validation in code (User Input Vs Entity Annotation).
tony.herstell1 Jul 24, 2008 3:26 PMI am letting the user type in an email on the the screen to add to the Mailing List.
/** * The email address */ @In @Out String email;
I am processing the simple string that comes back... and if its ok then adding to the list.
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public String addToList() {
log.info("Email to be added " + email);
InvalidValue[] invalidValues = Validators.instance().getValidator(MailingListEntry.class).getPotentialInvalidValues(email, email);
if (invalidValues.length != 0) {
facesMessages.addToControlFromResourceBundle(invalidValues[0].getPropertyName(), FacesMessage.SEVERITY_ERROR, invalidValues[0].getMessage());
} else {
Query query = em.createQuery("from MailingListEntry m where m.email = :email").setParameter("email", email);
if (query.getResultList().isEmpty()) {
log.info("Email not found in DBase.");
MailingListEntry mailingListEntry = new MailingListEntry();
mailingListEntry.setEmail(email);
try {
em.persist(mailingListEntry);
} catch (InvalidStateException ise) {
facesMessages.addToControlFromResourceBundle("mailingListEmailAddress", FacesMessage.SEVERITY_ERROR, "mailing_list_email_invalid");
}
facesMessages.addToControlFromResourceBundle("mailingListEmailAddress", FacesMessage.SEVERITY_INFO, "mailing_list_email_added");
} else {
log.info("Email already exists.");
facesMessages.addToControlFromResourceBundle("mailingListEmailAddress", FacesMessage.SEVERITY_ERROR, "mailing_list_already_created");
}
}
return null;
}
As you can see I have tried to use the Validator routine, to help validate it to no avail... (validation using Hib's Email one)
@NotNull(message="required")
@Email
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
SO as you ALSO see I though well just catch the exception when it fails to be added to DBase..
but I get this:
01:17:36,728 ERROR [AssertionFailure] an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session) org.hibernate.AssertionFailure: null id in nz.co.selwynequestriancentre.model.entity.MailingListEntry entry (don't flush the Session after an exception occurs) at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:55)
Humm... any ideas?