001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cmp2.fkmapping.ejb;
023:
024: import javax.ejb.EntityBean;
025: import javax.ejb.EntityContext;
026: import javax.ejb.RemoveException;
027: import javax.ejb.CreateException;
028:
029: /**
030: * @ejb.bean
031: * name="Child"
032: * type="CMP"
033: * cmp-version="2.x"
034: * view-type="local"
035: * reentrant="false"
036: * primkey-field="id"
037: * @ejb.pk generate="false"
038: * @ejb.util generate="physical"
039: * @ejb.persistence table-name="CHILD"
040: * @jboss.persistence
041: * create-table="true"
042: * remove-table="true"
043: * @ejb:transaction-type type="Container"
044: * @jboss.container-configuration name="INSERT after ejbPostCreate Container"
045: *
046: * @author <a href="mailto:alex@jboss.org">Alex Loubyansky</a>
047: */
048: public abstract class ChildCMPBean implements EntityBean {
049: // Attributes -----------------------------------------------
050: private EntityContext ctx;
051:
052: // CMP accessors --------------------------------------------
053: /**
054: * @ejb.pk-field
055: * @ejb.persistent-field
056: * @ejb.interface-method
057: * @ejb.persistence column-name="CHILD_ID"
058: */
059: public abstract Long getId();
060:
061: public abstract void setId(Long id);
062:
063: /**
064: * @ejb.persistent-field
065: * @ejb.interface-method
066: * @ejb.persistence column-name="FIRST_NAME"
067: */
068: public abstract String getFirstName();
069:
070: public abstract void setFirstName(String name);
071:
072: /**
073: * Non-null CMP field mapped to the foreign key field
074: * Used as a read-only field to verify correctness of INSERT
075: *
076: * @ejb.persistent-field
077: * @ejb.interface-method
078: * @ejb.persistence column-name="MOTHER_ID"
079: * @jboss.persistence not-null="true"
080: */
081: public abstract Long getMotherId();
082:
083: public abstract void setMotherId(Long id);
084:
085: /**
086: * Non-null CMP field mapped to the foreign key field
087: * Used as a read-only field to verify correctness of INSERT
088: *
089: * @ejb.persistent-field
090: * @ejb.interface-method
091: * @ejb.persistence column-name="MOTHER_NAME"
092: * @jboss.persistence not-null="true"
093: */
094: public abstract String getMotherName();
095:
096: public abstract void setMotherName(String name);
097:
098: /**
099: * @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: /**
117: * @ejb.interface-method
118: */
119: public abstract void setMother(ParentLocal parent);
120:
121: // EntityBean implementation -------------------------------------
122: /**
123: * @ejb.create-method
124: */
125: public Long ejbCreate(Long childId, String firstName)
126: throws CreateException {
127: setId(childId);
128: setFirstName(firstName);
129: return null;
130: }
131:
132: public void ejbPostCreate(Long id, String firstName) {
133: }
134:
135: /**
136: * @ejb.create-method
137: */
138: public Long ejbCreate(Long childId, String firstName,
139: Long parentId, String parentName) throws CreateException {
140: setId(childId);
141: setFirstName(firstName);
142: return null;
143: }
144:
145: public void ejbPostCreate(Long id, String firstName, Long parentId,
146: String parentName) throws CreateException {
147: ParentLocal parent = null;
148: try {
149: // this will trigger synchronization with the store and this, not yet
150: // created, child should not be updated
151: parent = ParentUtil.getLocalHome().findByPrimaryKey(
152: new ParentPK(parentId, parentName));
153: } catch (Exception e) {
154: throw new CreateException("Could not create relationship: "
155: + e.getMessage());
156: }
157:
158: setMother(parent);
159:
160: if (!id.equals(ctx.getPrimaryKey()))
161: throw new IllegalStateException(
162: "Primary key is not available in ejbPostCreate.");
163:
164: if (ctx.getEJBLocalObject() == null)
165: throw new IllegalStateException(
166: "Local object is not available in ejbPostCreate.");
167: }
168:
169: /**
170: * @param ctx The new entityContext value
171: */
172: public void setEntityContext(EntityContext ctx) {
173: this .ctx = ctx;
174: }
175:
176: /**
177: * Unset the associated entity context.
178: */
179: public void unsetEntityContext() {
180: this .ctx = null;
181: }
182:
183: public void ejbActivate() {
184: }
185:
186: public void ejbLoad() {
187: }
188:
189: public void ejbPassivate() {
190: }
191:
192: public void ejbRemove() throws RemoveException {
193: }
194:
195: public void ejbStore() {
196: }
197: }
|