KickJava   Java API By Example, From Geeks To Geeks.

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


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