View Javadoc
1   package info.ejava.examples.ejb.interceptor.bo;
2   
3   import java.io.Serializable;
4   import java.util.List;
5   
6   import javax.persistence.CascadeType;
7   import javax.persistence.Column;
8   import javax.persistence.Entity;
9   import javax.persistence.FetchType;
10  import javax.persistence.GeneratedValue;
11  import javax.persistence.Id;
12  import javax.persistence.NamedQueries;
13  import javax.persistence.NamedQuery;
14  import javax.persistence.OneToMany;
15  import javax.persistence.PrePersist;
16  import javax.persistence.PreUpdate;
17  import javax.persistence.Table;
18  import javax.validation.Valid;
19  import javax.validation.constraints.NotNull;
20  import javax.validation.constraints.Pattern;
21  import javax.validation.constraints.Size;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  @SuppressWarnings("serial")
27  @Entity
28  @Table(name="EJBINTERCEPTOR_CONTACT")
29  @NamedQueries({
30      @NamedQuery(name="EJBInterceptorContact.getContact", 
31          query="select distinct c from Contact c "
32                  + "left join fetch c.contactInfo "
33                  + "where c.normalizedName like :name")
34  })
35  public class Contact implements Serializable {
36      private static final Logger logger = LoggerFactory.getLogger(Contact.class);
37      
38      @Id @GeneratedValue
39      @Column(name="CONTACT_ID")
40      private int id;
41      
42      @NotNull(groups={PreNormalizedCheck.class, PrePersistCheck.class})
43      @Size(min=1, max=32, groups={PostNormalizedCheck.class, PrePersistCheck.class})
44      @Pattern.List({
45          @Pattern(regexp="^[A-Za-z0-9-\\ ]+$", groups=PreNormalizedCheck.class),
46          @Pattern(regexp="^([A-Z][a-z0-9-]+\\ *)+$", groups=PostNormalizedCheck.class)
47      })
48      @Column(name="CONTACT_NAME", length=32, nullable=false)
49      private String name;
50      
51      @Column(name="NORMALIZED_NAME", length=32, nullable=false)
52      private String normalizedName;
53      
54      @PrePersist
55      @PreUpdate
56      private void normalizeName() {
57          normalizedName = (name == null) ? null : name.toLowerCase().trim();
58          logger.debug(this.toString());
59      }
60      
61      @Valid
62      @OneToMany(fetch=FetchType.LAZY, mappedBy="contact", orphanRemoval=true, 
63          cascade={CascadeType.PERSIST, CascadeType.REMOVE})
64      private List<ContactInfo> contactInfo;
65  
66      
67      public int getId() { return id; }
68  
69      public String getName() { return name; }
70      public void setName(String name) {
71          this.name = name;
72      }
73      public Contact withName(String name) {
74          setName(name);
75          return this;
76      }
77      
78      public List<ContactInfo> getContactInfo() {
79          return contactInfo;
80      }
81  
82      @Override
83      public String toString() {
84          StringBuilder builder = new StringBuilder();
85          builder.append("Contact [id=").append(id)
86              .append(", name=").append(name)
87              .append(", normalizedName=").append(normalizedName)
88              .append("]");
89          return builder.toString();
90      }
91      
92      
93  }