-
1. Re: EntityHome and validation
wark2007 Aug 13, 2008 10:46 AM (in response to wark2007)Anybody can help please? I have absolutely no idea how to solve that problem in a seam manner.
-
2. Re: EntityHome and validation
sjmenden Aug 13, 2008 2:40 PM (in response to wark2007)Change:
@Override @Begin public void create() { super.create(); }
To:
@Override @Begin(flushMode=FlushModeType.MANUAL) public void create() { super.create(); }
That will prevent the automatic flushing. Now, think of how you are going to architect this next. The first reaction might be to override the update and persist methods and remove the entityManager.flush(); method, but remember, it is needed for the persist method to actually set the ID, and the flush is the only effective line in the update method, so you should rearchitect your view or add more buttons.
The other and better option, now that I am reading your original post again, is to forget everything I said above, and just override the persist method like so:
@Transactional public String persist() { //Do your additional validation here before persisting getEntityManager().persist( getInstance() ); getEntityManager().flush(); assignId( PersistenceProvider.instance().getId( getInstance(), getEntityManager() ) ); createdMessage(); raiseAfterTransactionSuccessEvent(); return "persisted"; }
Also, have you considered writing your own hibernate validators?
-
3. Re: EntityHome and validation
jguglielmin Aug 13, 2008 4:06 PM (in response to wark2007)Within your custom validator, you could create an instance of import org.jboss.seam.ui.validator.ModelValidator and invoke the model validation. You can put it before or after your own validation and it uses the hibernate annotations on your entity bean (if this is what you are looking for).
-
4. Re: EntityHome and validation
wark2007 Aug 14, 2008 10:20 AM (in response to wark2007)Thank you very much! That was a good idea. Unfortunately, it didn't work for me but you gave me an idea how to solve it.
The problem was caused by invoking the setters of the entity when a form was submitted. With the code snippet above you gave me an idea how to solve it. It is really simple:
public String update() { if (!valid()) { // rollback the updates caused by the setters Transaction.instance.rollback(); } super.update(); }
I hope that this solution do not have any bad side effects. What do you think of this solution?
-
5. Re: EntityHome and validation
wark2007 Aug 14, 2008 10:26 AM (in response to wark2007)Nice idea with the custom validator. Unfortunately, I couldn't find good documentation about that topic in a rush. Do you mean implementing the custom validation by using the EntityListeners like described here?
Do you have any good bookmarks regarding that topic?
-
6. Re: EntityHome and validation
leesy Nov 25, 2008 5:58 PM (in response to wark2007)Bit of a bump for this topic as I'm experiencing the same troubles but the suggested fixes aren't working for me. Does anyone else have any other suggestions to get past this problem. Here's my situation:
I've got an page with values bound to my EntityHome. That contains an update method like below:
public String update() { boolean errorsFound = false; if (StringUtils.isBlank(getInstance().getExternalId())) { errorsFound = true; getFacesMessages().addToControlFromResourceBundleOrDefault( "selectedProjectExternalId", SEVERITY_ERROR, "sso.manager.project.blankid", "Please enter an external ID for the project."); } // More checks for errors here if (errorsFound) { return FAILURE.toString(); } return super.update(); }
If I enter values that fail the validation, the error messages are shown but the database seems to still be updated to match the now incorrect data in the instance. All the conversations in my application are started with FlushMode set to true (via pages.xml) so I'm not 100% why this is happening.
-
7. Re: EntityHome and validation
coolstone Jul 1, 2009 9:20 AM (in response to wark2007)I have the same problem, how to validate before update?For example, to check out whether some name is repeated in a table.
-
8. Re: EntityHome and validation
noonereally Jul 1, 2009 4:45 PM (in response to wark2007)This morning, I was reading about entity lifecycle listeners. You can listen for lifecycle callbacks using annotations such as @PrePersist. The callback method could be in the entity or in a separate class that's specified using the @EntityListeners annotation on the entity. The argument that's passed to the listener is a reference to the bean. Inside that callback method, you could do any validation that might be needed.
I have to admit that I've never used an entity lifecycle listener for validation, so I don't really know if this is a good idea. I'm just throwing it out there to see if anyone has any thoughts on this. I like the idea, because it allows you to avoid changing any of your existing code or code that's provided by Seam.