KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > enterprise > customer_cmp_ejb > persistence > Customer


1 /*
2  * Customer.java
3  *
4  * Created on November 24, 2005, 7:40 PM
5  *
6  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
7  * Use is subject to license terms.
8  */

9
10 /**
11  *
12  * @author Rahul Biswas
13  */

14
15
16 package enterprise.customer_cmp_ejb.persistence;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import javax.persistence.*;
22 import javax.persistence.OneToMany;
23
24
25
26 @Entity
27 @NamedQueries(
28     value={@NamedQuery(name="findCustomerByFirstName", query="select object(c) from Customer c where c.firstName= :firstName"),
29     @NamedQuery(name="findCustomerByLastName", query="select object(c) from Customer c where c.lastName= :lastName")}
30 )
31
32 public class Customer implements java.io.Serializable JavaDoc{
33
34     //access methods for cmp fields
35
private String JavaDoc id;
36     private String JavaDoc firstName;
37     private String JavaDoc lastName;
38     private Collection JavaDoc<Address> addresses;
39     private Collection JavaDoc<Subscription> subscriptions;
40     
41     public Customer(){
42         
43     }
44     
45     @Id
46     @Column(name="customerid")
47     public String JavaDoc getCustomerID(){ //primary key
48
return id;
49     }
50     public void setCustomerID(String JavaDoc id){
51         this.id=id;
52     }
53     
54     public String JavaDoc getFirstName(){
55         return firstName;
56     }
57     public void setFirstName(String JavaDoc firstName){
58         this.firstName=firstName;
59     }
60
61     public String JavaDoc getLastName(){
62         return lastName;
63     }
64     public void setLastName(String JavaDoc lastName){
65         this.lastName=lastName;
66     }
67
68     public Customer(String JavaDoc id, String JavaDoc firstName, String JavaDoc lastName) {
69         setCustomerID(id);
70         setFirstName(firstName);
71         setLastName(lastName);
72     }
73
74
75     @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
76     //targetEntity not needed as the property is a generic java data type
77
//cascade will default to none, needs to be ALL because we need to save newly added addresses
78
//fetch specified as EAGER, because we need to access the association outside of a txn.
79
//mapped by is not needed because this is a unidirectional mapping
80
public Collection JavaDoc<Address> getAddresses(){
81         return addresses;
82     }
83     public void setAddresses (Collection JavaDoc<Address> addresses){
84         this.addresses=addresses;
85     }
86
87     @ManyToMany(fetch=FetchType.EAGER )
88     @JoinTable(
89             name="CUSTOMERBEANSUBSCRIPTIONBEAN",
90             joinColumns=@JoinColumn(name="CUSTOMERBEAN_CUSTOMERID96", referencedColumnName="customerid"),
91             inverseJoinColumns=@JoinColumn(name="SUBSCRIPTIONBEAN_TITLE", referencedColumnName="TITLE")
92     )
93     public Collection JavaDoc<Subscription> getSubscriptions(){
94         return subscriptions;
95     }
96     public void setSubscriptions (Collection JavaDoc<Subscription> subscriptions){
97         this.subscriptions=subscriptions;
98     }
99
100     //business methods
101
//We could have as well used a java.util.List for a collection of addresses.
102
//But what's below is only for demonstration purposes and the use of Collection instead of List is for that purpose only.
103
@Transient
104     //since the signature starts with a get, need to annotate it as @Transient
105
public ArrayList JavaDoc getAddressList() {
106         ArrayList JavaDoc list = new ArrayList JavaDoc();
107         Iterator JavaDoc c = getAddresses().iterator();
108         while (c.hasNext()) {
109             list.add((Address)c.next());
110         }
111         return list;
112     }
113
114     @Transient
115     public ArrayList JavaDoc getSubscriptionList() {
116         ArrayList JavaDoc list = new ArrayList JavaDoc();
117         Iterator JavaDoc c = getSubscriptions().iterator();
118         while (c.hasNext()) {
119             list.add((Subscription)c.next());
120         }
121         return list;
122     }
123
124     
125     // other EntityBean methods
126
@PostPersist
127     public void postCreate (){
128         System.out.println("Customer::postCreate:");
129     }
130
131
132     
133     @PostRemove
134     public void ejbRemove() {
135         System.out.println("Customer::postRemove");
136     }
137     
138
139 }
140
Popular Tags