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.entity.beans;
023:
024: import java.sql.Connection;
025: import java.sql.Statement;
026:
027: import javax.ejb.CreateException;
028: import javax.ejb.EJBException;
029: import javax.ejb.EntityBean;
030:
031: import javax.naming.InitialContext;
032:
033: import javax.sql.DataSource;
034:
035: import org.jboss.test.entity.interfaces.TestEntityValue;
036:
037: /**
038: * An entity.
039: *
040: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
041: * @version $Revision: 57211 $
042: *
043: * @ejb:bean
044: * name="TestEntity"
045: * type="CMP"
046: * view-type="both"
047: * jndi-name="test/entity/TestEntity"
048: * local-jndi-name="test/entity/TestEntityLocal"
049: * schema="test"
050: * primkey-field="entityID"
051: * @ejb:pk
052: * class="java.lang.String"
053: * @ejb:transaction
054: * type="Required"
055: * @ejb:value-object
056: *
057: * @jboss:container-configuration
058: * name="TestEntity Container Configuration"
059: * @jboss:table-name
060: * table-name="test_entity_testentity"
061: * @jboss:create-table
062: * create="true"
063: * @jboss:remove-table
064: * remove="true"
065: */
066: public abstract class TestEntityBean implements EntityBean {
067: /**
068: * @ejb:create-method
069: */
070: public String ejbCreate(TestEntityValue value)
071: throws CreateException {
072: setEntityID(value.getEntityID());
073: setValue1(value.getValue1());
074: return null;
075: }
076:
077: public void ejbPostCreate(TestEntityValue value)
078: throws CreateException {
079: }
080:
081: /**
082: * @ejb:interface-method
083: * @ejb:persistent-field
084: */
085: public abstract String getEntityID();
086:
087: public abstract void setEntityID(String entityID);
088:
089: /**
090: * @ejb:interface-method
091: * @ejb:persistent-field
092: * @jboss:method-attributes read-only="true"
093: */
094: public abstract String getValue1();
095:
096: /**
097: * @ejb:interface-method
098: */
099: public abstract void setValue1(String value1);
100:
101: /**
102: * @ejb:home-method
103: */
104: public void ejbHomeRemoveExternal(String entityID) {
105: Connection connection = null;
106: Statement statement = null;
107: try {
108: DataSource dataSource = (DataSource) new InitialContext()
109: .lookup("java:/DefaultDS");
110: connection = dataSource.getConnection();
111: statement = connection.createStatement();
112: int rows = statement
113: .executeUpdate("delete from test_entity_testentity "
114: + "where entityID = '" + entityID + "'");
115: if (rows != 1)
116: throw new Exception("Wrong number of rows deleted: "
117: + rows);
118: } catch (Exception e) {
119: throw new EJBException(e);
120: } finally {
121: try {
122: if (statement != null)
123: statement.close();
124: if (connection != null)
125: connection.close();
126: } catch (Exception e) {
127: throw new EJBException(e);
128: }
129: }
130: }
131:
132: /**
133: * @ejb:home-method
134: */
135: public void ejbHomeChangeValue1(String entityID, String value1) {
136: Connection connection = null;
137: Statement statement = null;
138: try {
139: DataSource dataSource = (DataSource) new InitialContext()
140: .lookup("java:/DefaultDS");
141: connection = dataSource.getConnection();
142: statement = connection.createStatement();
143: int rows = statement
144: .executeUpdate("update test_entity_testentity set value1 = '"
145: + value1
146: + "' where entityID = '"
147: + entityID + "'");
148: if (rows != 1)
149: throw new Exception("Wrong number of rows updated: "
150: + rows);
151: } catch (Exception e) {
152: throw new EJBException(e);
153: } finally {
154: try {
155: if (statement != null)
156: statement.close();
157: if (connection != null)
158: connection.close();
159: } catch (Exception e) {
160: throw new EJBException(e);
161: }
162: }
163: }
164: }
|