KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > fkmapping > ejb > ChildCMPBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.fkmapping.ejb;
23
24
25 import javax.ejb.EntityBean JavaDoc;
26 import javax.ejb.EntityContext JavaDoc;
27 import javax.ejb.RemoveException JavaDoc;
28 import javax.ejb.CreateException JavaDoc;
29
30
31 /**
32  * @ejb.bean
33  * name="Child"
34  * type="CMP"
35  * cmp-version="2.x"
36  * view-type="local"
37  * reentrant="false"
38  * primkey-field="id"
39  * @ejb.pk generate="false"
40  * @ejb.util generate="physical"
41  * @ejb.persistence table-name="CHILD"
42  * @jboss.persistence
43  * create-table="true"
44  * remove-table="true"
45  * @ejb:transaction-type type="Container"
46  * @jboss.container-configuration name="INSERT after ejbPostCreate Container"
47  *
48  * @author <a HREF="mailto:alex@jboss.org">Alex Loubyansky</a>
49  */

50 public abstract class ChildCMPBean
51    implements EntityBean JavaDoc
52 {
53    // Attributes -----------------------------------------------
54
private EntityContext JavaDoc ctx;
55
56    // CMP accessors --------------------------------------------
57
/**
58     * @ejb.pk-field
59     * @ejb.persistent-field
60     * @ejb.interface-method
61     * @ejb.persistence column-name="CHILD_ID"
62     */

63    public abstract Long JavaDoc getId();
64    public abstract void setId(Long JavaDoc id);
65
66    /**
67     * @ejb.persistent-field
68     * @ejb.interface-method
69     * @ejb.persistence column-name="FIRST_NAME"
70     */

71    public abstract String JavaDoc getFirstName();
72    public abstract void setFirstName(String JavaDoc name);
73
74    /**
75     * Non-null CMP field mapped to the foreign key field
76     * Used as a read-only field to verify correctness of INSERT
77     *
78     * @ejb.persistent-field
79     * @ejb.interface-method
80     * @ejb.persistence column-name="MOTHER_ID"
81     * @jboss.persistence not-null="true"
82     */

83    public abstract Long JavaDoc getMotherId();
84    public abstract void setMotherId(Long JavaDoc id);
85
86    /**
87     * Non-null CMP field mapped to the foreign key field
88     * Used as a read-only field to verify correctness of INSERT
89     *
90     * @ejb.persistent-field
91     * @ejb.interface-method
92     * @ejb.persistence column-name="MOTHER_NAME"
93     * @jboss.persistence not-null="true"
94     */

95    public abstract String JavaDoc getMotherName();
96    public abstract void setMotherName(String JavaDoc name);
97
98    /**
99     * @ejb.interface-method
100     * @ejb.relation
101     * name="Mother-Child"
102     * role-name="Child-has-Mother"
103     * target-ejb="Parent"
104     * target-role-name="Mother-has-Child"
105     * target-multiple="yes"
106     * cascade-delete="no"
107     * @jboss.relation
108     * related-pk-field="id"
109     * fk-column="MOTHER_ID"
110     * @jboss.relation
111     * related-pk-field="firstName"
112     * fk-column="MOTHER_NAME"
113     */

114    public abstract ParentLocal getMother();
115    /**
116     * @ejb.interface-method
117     */

118    public abstract void setMother(ParentLocal parent);
119
120    // EntityBean implementation -------------------------------------
121
/**
122     * @ejb.create-method
123     */

124    public Long JavaDoc ejbCreate(Long JavaDoc childId, String JavaDoc firstName)
125       throws CreateException JavaDoc
126    {
127       setId(childId);
128       setFirstName(firstName);
129       return null;
130    }
131
132    public void ejbPostCreate(Long JavaDoc id, String JavaDoc firstName) {}
133
134    /**
135     * @ejb.create-method
136     */

137    public Long JavaDoc ejbCreate(Long JavaDoc childId, String JavaDoc firstName, Long JavaDoc parentId, String JavaDoc parentName)
138       throws CreateException JavaDoc
139    {
140       setId(childId);
141       setFirstName(firstName);
142       return null;
143    }
144
145    public void ejbPostCreate(Long JavaDoc id, String JavaDoc firstName, Long JavaDoc parentId, String JavaDoc parentName)
146       throws CreateException JavaDoc
147    {
148       ParentLocal parent = null;
149       try
150       {
151          // this will trigger synchronization with the store and this, not yet
152
// created, child should not be updated
153
parent = ParentUtil.getLocalHome().findByPrimaryKey(new ParentPK(parentId, parentName));
154       }
155       catch(Exception JavaDoc e)
156       {
157          throw new CreateException JavaDoc("Could not create relationship: " + e.getMessage());
158       }
159
160       setMother(parent);
161
162       if(!id.equals(ctx.getPrimaryKey()))
163          throw new IllegalStateException JavaDoc("Primary key is not available in ejbPostCreate.");
164
165       if(ctx.getEJBLocalObject() == null)
166          throw new IllegalStateException JavaDoc("Local object is not available in ejbPostCreate.");
167    }
168
169    /**
170     * @param ctx The new entityContext value
171     */

172    public void setEntityContext(EntityContext JavaDoc ctx)
173    {
174       this.ctx = ctx;
175    }
176
177    /**
178     * Unset the associated entity context.
179     */

180    public void unsetEntityContext()
181    {
182       this.ctx = null;
183    }
184
185    public void ejbActivate() {}
186    public void ejbLoad() {}
187    public void ejbPassivate() {}
188    public void ejbRemove() throws RemoveException JavaDoc {}
189    public void ejbStore() {}
190 }
191
Popular Tags