1

Is custom constraints the way to go or is there a better approach? Before persisting the class/entity below I want to make sure.

(!(a == null && b == null && c == null))

public class Foo{
   String a; 
   String b; 
   String c;
}

How to do this with Hibernate/Hibernate validator?

Edit: @NotNull on fields are not good enough. They can all be only but no the same time.

Hardy
  • 18,659
  • 3
  • 49
  • 65
pethel
  • 5,397
  • 12
  • 55
  • 86
  • there is an annotation for non-null constraints. you probably should work through a validator tutorial first. – kostja Nov 05 '13 at 08:14
  • Yes but they can all be null if they are not null(all three) the same time. – pethel Nov 05 '13 at 08:21
  • 1
    Ah, sorry for not reading properly. You could probably find useful info here: http://stackoverflow.com/questions/1972933/cross-field-validation-with-hibernate-validator-jsr-303 and http://stackoverflow.com/questions/11890334/cross-field-validation-with-hibernatevalidator-works-fine-but-displays-no-error – kostja Nov 05 '13 at 08:29

1 Answers1

0

If you want to stick to the Bean Validation standard you would need to create custom class level constraint.

If you are happy with Hibernate Validator specific features, you could use a @ScriptAssert.

Hardy
  • 18,659
  • 3
  • 49
  • 65