2012-12-21 84 views
7

我有一个电子邮件收集财产的人的实体:如何对Embeddable实体属性强制执行验证约束?

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

在我的电子邮件类,我试图诠释电子邮件@Email:

@Embeddable 
public class EmailU implements Serializable { 
    private String email; 

    public EmailU(){ 
    } 

    @Email 
    public String getEmail() { 
     return email; 
    } 
} 

但它不工作。我的方法应该是什么?

回答

12

添加一个@Valid注释到您的集合属性。这会触发验证提供程序验证集合中的每个项目,然后将调用您的验证程序@Email

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

注释文档:http://docs.oracle.com/javaee/6/api/javax/validation/Valid.html