How to enforce a check constraint on an object property of an embedded object?

I have a Person object with an email collection property:

@ElementCollection @CollectionTable(schema="u",name="emails", joinColumns=@JoinColumn (name="person_fk")) @AttributeOverrides({ @AttributeOverride(name="email", column=@Column (name="email",nullable=false)), }) public List<EmailU> getEmails() { return emails; } 

In my email class, I tried to annotate email using @Email:

 @Embeddable public class EmailU implements Serializable { private String email; public EmailU(){ } @Email public String getEmail() { return email; } } 

But that will not work. What should be my approach here?

+7
source share
1 answer

Add the @Valid annotation to the collection property. This forces your validation provider to check each item in the collection, which then calls your @Email validator.

 @Valid @ElementCollection @CollectionTable(schema="u",name="emails", joinColumns=@JoinColumn (name="person_fk")) @AttributeOverrides({ @AttributeOverride(name="email", column=@Column (name="email",nullable=false)), }) public List<EmailU> getEmails() { return emails; } 

Documentation for annotation: http://docs.oracle.com/javaee/6/api/javax/validation/Valid.html

+12
source

All Articles