[jboss-user] [JBoss Seam] - Re: Domain model duplicates validation
fernando_jmt
do-not-reply at jboss.com
Mon May 28 11:47:22 EDT 2007
Hi.
anonymous wrote :
| But in some weird case where my validation require some kind of twisted logic using some properties from the Person entity and all the other entities already persisted, is there a clean way to add this logic in the model? Or this is conceptually wrong?
|
For me, putting validation logic in your model is nasty. And it's conceptually wrong. If you want to validate something related to one or several entities, and you don't have another way to do it (according your requirements) then you should perfom such validation in your controller (SLSB or SFSB).
The example I gave you it was recently adopted by me for validate duplicates for an entity (of course when the properties are not the @Ids). Time ago I was doing this validation in a session bean, something as follows:
| public void persis(Person entity){
|
| try {
| em.createQuery("select p from P where p.lastName=:lastName and
| p.firstName = :firstName).setParameter("lastName",
| entity.lastName).setParameter("firstName",
| entity.firstName).getSingleResult();
|
| //user exists, then throw some AlreadyExistException.
| } catch (NoResultException e) {
| em.persist(entity);
| }
|
| }
|
But the above code involves two SQL's:
| select from person ...where lastname....
| insert into person...... (if the above sql does not return value)
|
But delegating this validation to DB layer (@UniqueConstraint) the only SQL will always be:
| insert into person....
|
So, in order to reduce the DB round trip I decided to use DB approach for this case. Of course, if you are free to make changes in the database schema. I think this is total portable, because I don't know some database server that does not allow to use "UNIQUE" constraints.
anonymous wrote : The validation you gave me works great. The only complain is that a get some error logs about:
Yes, this is annoying, I ask the same time ago, see below link (I din't try it yet myself):
http://www.jboss.com/index.html?module=bb&op=viewtopic&t=98560
anonymous wrote :
| Hey, i'm your neighbor! From Paraguay :D
|
Nice to hear it.
Cheers.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4049075#4049075
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4049075
More information about the jboss-user
mailing list