2012-12-21 35 views
7

Tôi có một thực thể Person với một tài sản thu Email:Làm cách nào để thực thi ràng buộc xác thực trên thuộc tính của thực thể có thể nhúng?

@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; 
} 

Trong lớp Email của tôi, tôi đã cố gắng để chú thích email với @Email:

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

    public EmailU(){ 
    } 

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

Nhưng nó không hoạt động. Cách tiếp cận của tôi ở đây là gì?

Trả lời

12

Thêm chú thích @Valid vào thuộc tính bộ sưu tập của bạn. Điều này kích hoạt nhà cung cấp xác thực của bạn để xác thực từng mục trong bộ sưu tập, sau đó sẽ gọi trình xác thực @Email của bạn.

@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; 
} 

Chú Tài liệu: http://docs.oracle.com/javaee/6/api/javax/validation/Valid.html

Các vấn đề liên quan