-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: No automic validation on Collection.remove(item)?
PostPosted: Tue Sep 14, 2010 3:50 pm 
Newbie

Joined: Tue Sep 14, 2010 3:35 pm
Posts: 2
I use Validator 4.1.0 with annotations together with hibernate. I realized that the validation events only get fired when I save or update the entity in question.

However, in the following scenario the validation events don't fire:

EntityA is associated by means of a collection to EntityB as follows:

Code:
@ManyToMany(fetch = FetchType.LAZY)
   @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
   @Size(min = 1)
   public Set<EntityB> getBs() {
      return bs;
   }


The @Size(min = 1) annotation only is considered when I save/update EntityA directly. But when I call

Code:
entityA.getBs().remove(theLastOfTheBs);
session.commit();


the validation event does not get fired. It get's fired however when I call session.save(entityA);.

Is there a way to fire the event without the unnecessary call to save()?

Conny


Top
 Profile  
 
 Post subject: Re: No automic validation on Collection.remove(item)?
PostPosted: Fri Sep 17, 2010 10:21 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Have you tried to modify your cascade types? Try CascadeType.ALL or at least add CascadeType.REMOVE

--Hardy


Top
 Profile  
 
 Post subject: Re: No automic validation on Collection.remove(item)?
PostPosted: Sat Sep 18, 2010 3:42 am 
Newbie

Joined: Tue Sep 14, 2010 3:35 pm
Posts: 2
I tried it. But it does not lead to the desired outcome.

I've also just realized, that it is not the session.save(entityA) that triggers the validation but the change of some other property of entityA (in my case a String property). The save() is actually unnecessary. It is necessary, however, to modify some simple attributes of the entity in order to trigger the validation of the association.

So I restate my question: can the validation of the collection be achieved without modifying the entity in some other way?


Top
 Profile  
 
 Post subject: Re: No automic validation on Collection.remove(item)?
PostPosted: Tue Sep 21, 2010 6:27 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Looking at BeanValidationEventListener

Code:
public class BeanValidationEventListener implements
      PreInsertEventListener, PreUpdateEventListener, PreDeleteEventListener, Initializable {
...
}


there is no validation on collection change events. I think the reason is that JPA only defines the Bean Validation callbacks for these entity changes. Hibernate also has for example a PreCollectionUpdateEvent. You could just write your own event listener (maybe even extending BeanValidationEventListener) and handle collection change events as well.
You could also create a Jira for this. It seems to make sense to add this listeners as well, but of course this has to be compliant with the spec. Maybe some sort of option would make sense here.

--Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.