2012-12-21 76 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