-->
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: Only first constraint violation in collection reported?
PostPosted: Thu Aug 12, 2010 1:58 pm 
Newbie

Joined: Thu Aug 12, 2010 1:50 pm
Posts: 2
Hopefully this is the right place to ask; if not, a pointer to the correct forum would be appreciated... I have a List<T> attribute that only gives one ConstraintViolation, even when more than one of its members should fail validation...

Code:
//City.java
@Entity
@Table(name="cities")
@GroupSequence({Basic.class, PointOfInterest.class, Extended.class})
public class PointOfInterest implements Serializable {
  @XmlElement(name = "Name", required=true)
  @OneToMany(mappedBy="point", fetch=FetchType.EAGER, cascade={CascadeType.ALL})
  @Size(min=1, groups=Basic.class)
  @Valid
  private List<Name> names = new ArrayList<Name>();
  //...

//Name.java
@Entity
@Table(name="city_nm")
@GroupSequence({Basic.class, Name.class, Extended.class})
public class Name implements Serializable {
  @XmlAttribute
  @NotNull(groups=Basic.class)
  @Size(min=2, max=250, groups=Basic.class)
  @Column(name="city_nm", nullable=false, length=250)
  private String name;
  //...

//CityTests.java
@Test
public void multipleInvalidNames() throws Exception {
  City city = factory.makeCity();
  Name name1 = factory.makeName();
  name1.setName("a"); //Below @Size min!
  city.addName(name1);
  Name name2 = factory.makeName();
  name2.setName("b"); //Also too short!
  city.addName(name2);
  Set<ConstraintViolation<?>> errors =
tryPersist(city).getConstraintViolations();
  //...


errors.toString() gives me:
[ConstraintViolationImpl{interpolatedMessage='size must be between 2
and 250', propertyPath=name, rootBeanClass=class
com.my.city.domain.Name,
messageTemplate='{javax.validation.constraints.Size.message}'}]

...so that first Name seems to have been flagged as too short, but I'd have expected a second ConstraintViolation for the second name. Should I have gotten one?

Many thanks for any advice/leads anyone can offer!


Top
 Profile  
 
 Post subject: Re: Only first constraint violation in collection reported?
PostPosted: Fri Aug 13, 2010 7:55 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

You should get two violations. Which version of Validator are you using. To help narrowing down you should try to validate a City instance directly and bypass the JPA/Hibernate integration. Try something like this to get a Validator instance:
Code:
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<City>> violations = validator.validate(city);

That said, your example seems a little inconsistent. You are talking about City, but the class you posted is PointOfInterest. Also in the property path of the violation I would expect an index, eg name[0].
Once we isolate the problem we can go further. Maybe you could also create a testcase which we could use to investigate the problem.

--Hardy


Top
 Profile  
 
 Post subject: Re: Only first constraint violation in collection reported?
PostPosted: Wed Aug 18, 2010 6:33 pm 
Newbie

Joined: Thu Aug 12, 2010 1:50 pm
Posts: 2
Hardy,

Many thanks for the help (and my apologies for my delayed response)... I'm using Validator 3.0-JBoss-4.0.2 (as included with Glassfish 3 JEE 6 version).

Quote:
That said, your example seems a little inconsistent. You are talking about City, but the class you posted is PointOfInterest.


Quite right, I was trying to obtain a forum-friendly example by paring down my actual code, and apparently I made some mistakes in my search-and-replace. PointOfInterest is the actual class name (just rolls off the tongue, doesn't it?), not City. The propertyPaths I posted, however, are exactly as they appeared on the ConstraintViolations I got (but see below).

Quote:
Also in the property path of the violation I would expect an index, eg name[0].


Indeed, once I called validator.validate(poi), not only did I get the additional ConstraintViolation I was looking for, but the propertyPaths changed. Here's what errors.toString() gives me now:

Code:
[ConstraintViolationImpl{interpolatedMessage='size must be between 2 and 250', propertyPath=names[1].name, rootBeanClass=class com.my.poi.domain.PointOfInterest, messageTemplate='{javax.validation.constraints.Size.message}'}, ConstraintViolationImpl{interpolatedMessage='size must be between 2 and 250', propertyPath=names[0].name, rootBeanClass=class com.my.poi.domain.PointOfInterest, messageTemplate='{javax.validation.constraints.Size.message}'}]


So I guess my problem does stem from the JPA/Hibernate integration. What can I do to work around this? Call validator.validate(object) manually in my PointOfInterestService's save() method? (I'd like the validation to be automatic if possible.)

hardy.ferentschik wrote:
Hi,

You should get two violations. Which version of Validator are you using. To help narrowing down you should try to validate a City instance directly and bypass the JPA/Hibernate integration. Try something like this to get a Validator instance:
Code:
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<City>> violations = validator.validate(city);

That said, your example seems a little inconsistent. You are talking about City, but the class you posted is PointOfInterest. Also in the property path of the violation I would expect an index, eg name[0].
Once we isolate the problem we can go further. Maybe you could also create a testcase which we could use to investigate the problem.

--Hardy


Top
 Profile  
 
 Post subject: Re: Only first constraint violation in collection reported?
PostPosted: Thu Aug 19, 2010 4:29 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
It would help to know what your tryPersist() does and the hibernate log would be good as well. Are you using merge() within tryPersist() ? Using manual validation is of course always an option.

--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.