KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > entity > beans > TestEntityBean


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.entity.beans;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.Statement JavaDoc;
26
27 import javax.ejb.CreateException JavaDoc;
28 import javax.ejb.EJBException JavaDoc;
29 import javax.ejb.EntityBean JavaDoc;
30
31 import javax.naming.InitialContext JavaDoc;
32
33 import javax.sql.DataSource JavaDoc;
34
35 import org.jboss.test.entity.interfaces.TestEntityValue;
36
37 /**
38  * An entity.
39  *
40  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
41  * @version $Revision: 37406 $
42  *
43  * @ejb:bean
44  * name="TestEntity"
45  * type="CMP"
46  * view-type="both"
47  * jndi-name="test/entity/TestEntity"
48  * local-jndi-name="test/entity/TestEntityLocal"
49  * schema="test"
50  * primkey-field="entityID"
51  * @ejb:pk
52  * class="java.lang.String"
53  * @ejb:transaction
54  * type="Required"
55  * @ejb:value-object
56  *
57  * @jboss:container-configuration
58  * name="TestEntity Container Configuration"
59  * @jboss:table-name
60  * table-name="test_entity_testentity"
61  * @jboss:create-table
62  * create="true"
63  * @jboss:remove-table
64  * remove="true"
65  */

66 public abstract class TestEntityBean
67    implements EntityBean JavaDoc
68 {
69    /**
70     * @ejb:create-method
71     */

72    public String JavaDoc ejbCreate(TestEntityValue value)
73      throws CreateException JavaDoc
74    {
75       setEntityID(value.getEntityID());
76       setValue1(value.getValue1());
77       return null;
78    }
79
80    public void ejbPostCreate(TestEntityValue value)
81       throws CreateException JavaDoc
82    {
83    }
84
85    /**
86     * @ejb:interface-method
87     * @ejb:persistent-field
88     */

89    public abstract String JavaDoc getEntityID();
90    public abstract void setEntityID(String JavaDoc entityID);
91
92    /**
93     * @ejb:interface-method
94     * @ejb:persistent-field
95     * @jboss:method-attributes read-only="true"
96     */

97    public abstract String JavaDoc getValue1();
98
99    /**
100     * @ejb:interface-method
101     */

102    public abstract void setValue1(String JavaDoc value1);
103
104    /**
105     * @ejb:home-method
106     */

107    public void ejbHomeRemoveExternal(String JavaDoc entityID)
108    {
109       Connection JavaDoc connection = null;
110       Statement JavaDoc statement = null;
111       try
112       {
113          DataSource JavaDoc dataSource = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:/DefaultDS");
114          connection = dataSource.getConnection();
115          statement = connection.createStatement();
116          int rows = statement.executeUpdate("delete from test_entity_testentity "
117                                             + "where entityID = '" + entityID + "'");
118          if (rows != 1)
119             throw new Exception JavaDoc("Wrong number of rows deleted: " + rows);
120       }
121       catch (Exception JavaDoc e)
122       {
123          throw new EJBException JavaDoc(e);
124       }
125       finally
126       {
127          try
128          {
129             if (statement != null)
130                statement.close();
131             if (connection != null)
132                connection.close();
133          }
134          catch (Exception JavaDoc e)
135          {
136             throw new EJBException JavaDoc(e);
137          }
138       }
139    }
140
141    /**
142     * @ejb:home-method
143     */

144    public void ejbHomeChangeValue1(String JavaDoc entityID, String JavaDoc value1)
145    {
146       Connection JavaDoc connection = null;
147       Statement JavaDoc statement = null;
148       try
149       {
150          DataSource JavaDoc dataSource = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:/DefaultDS");
151          connection = dataSource.getConnection();
152          statement = connection.createStatement();
153          int rows = statement.executeUpdate("update test_entity_testentity set value1 = '" + value1 +
154                                             "' where entityID = '" + entityID + "'");
155          if (rows != 1)
156             throw new Exception JavaDoc("Wrong number of rows updated: " + rows);
157       }
158       catch (Exception JavaDoc e)
159       {
160          throw new EJBException JavaDoc(e);
161       }
162       finally
163       {
164          try
165          {
166             if (statement != null)
167                statement.close();
168             if (connection != null)
169                connection.close();
170          }
171          catch (Exception JavaDoc e)
172          {
173             throw new EJBException JavaDoc(e);
174          }
175       }
176    }
177 }
178
Popular Tags