Mapping to a table I created + jbosscmp-jdbc
joycestack Feb 12, 2003 9:46 AMHello,
I have a client and an entity bean. All I want to figure out is inserting into a table that I created already in Hypersonic and not one that is dynamically created by JBoss. Here are my files.
package com.customers.ejb;
import java.rmi.*;
import javax.ejb.*;
import java.util.*;
public interface Customer extends EJBObject
{
public String getCustomerID() throws RemoteException;
public void setCustomerID(String pCustomerID) throws RemoteException ;
public String getFirstName() throws RemoteException ;
public void setFirstName(String pFirstName) throws RemoteException;
public String getLastName() throws RemoteException;
public void setLastName(String pLastName) throws RemoteException;
public String getContactName() throws RemoteException;
public void setContactName(String pContactName) throws RemoteException;
public String getPhoneNumber() throws RemoteException;
public void setPhoneNumber(String pPhoneNumber) throws RemoteException;
public Date getDOB() throws RemoteException;
public void setDOB(Date pDOB) throws RemoteException;
}
package com.customers.ejb;
import java.rmi.*;
import javax.ejb.*;
import java.util.*;
public interface CustomerHome extends EJBHome
{
public Customer create(String pCustomerID, String pFirstName, String pLastName,
String pContactName, String pPhoneNumber, Date pDOB) throws CreateException, RemoteException;
public Customer findByPrimaryKey(String pPrimaryKey) throws FinderException, RemoteException;
}
package com.customers.ejb;
import java.rmi.RemoteException;
import java.util.Date;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;
import javax.ejb.CreateException;
import java.util.*;
import java.sql.*;
import javax.sql.DataSource;
import javax.naming.*;
/**
* @author jstack
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public abstract class CustomerBean implements EntityBean {
private EntityContext entityContext = null;
/**
* Mandatory Default public cstor with no args
*/
public CustomerBean()
{
super();
}
/**
* @see javax.ejb.EntityBean#ejbActivate()
*/
public void ejbActivate() { System.out.println("ejbActivate()"); }
/**
* @see javax.ejb.EntityBean#ejbLoad()
*/
public void ejbLoad() { System.out.println("ejbLoad()"); }
/**
* @see javax.ejb.EntityBean#ejbPassivate()
*/
public void ejbPassivate() { System.out.println("ejbPassivate()"); }
/**
* @see javax.ejb.EntityBean#ejbRemove()
*/
public void ejbRemove() { System.out.println("ejbRemove()"); }
/**
* @see javax.ejb.EntityBean#ejbStore()
*/
public void ejbStore() { System.out.println("ejbStore()"); }
/**
* @see javax.ejb.EntityBean#setEntityContext(EntityContext)
*/
public void setEntityContext(EntityContext pContext)
{
System.out.println("setEntityContext()");
this.entityContext = pContext;
}
/**
* @see javax.ejb.EntityBean#unsetEntityContext()
*/
public void unsetEntityContext() { System.out.println("unsetEntityContext()"); }
// My Abstract Methods that tie me to the remote interface
public abstract String getCustomerID();
public abstract void setCustomerID(String pCustomerID);
public abstract String getFirstName();
public abstract void setFirstName(String pFirstName);
public abstract String getLastName();
public abstract void setLastName(String pLastName);
public abstract String getContactName();
public abstract void setContactName(String pContactName);
public abstract String getPhoneNumber();
public abstract void setPhoneNumber(String pPhoneNumber);
public abstract Date getDOB();
public abstract void setDOB(Date pDOB);
// Methods to tie in with my Home Interface
// To be implemented
public String ejbCreate(String pCustomerID, String pFirstName, String pLastName,
String pContactName, String pPhoneNumber, Date pDOB) throws CreateException
{
System.out.println("ejbCreate()");
System.out.println(pCustomerID);
System.out.println(pFirstName);
System.out.println(pLastName);
System.out.println(pContactName);
System.out.println(pPhoneNumber);
System.out.println(pDOB);
setCustomerID(pCustomerID);
setFirstName(pFirstName);
setLastName(pLastName);
setContactName(pContactName);
setPhoneNumber(pPhoneNumber);
setDOB(pDOB);
return null;
}
public void ejbPostCreate(String pCustomerID, String pFirstName, String pLastName,
String pContactName, String pPhoneNumber, Date pDOB) throws CreateException
{
System.out.println("ejbPostCreate()");
}
}
package com.customers.client;
import com.customers.ejb.Customer;
import com.customers.ejb.CustomerHome;
import java.util.*;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
public class Client_1
{
public static void main(String [] args)
{
try
{
Context jndiContext = getInitialContext();
Object ref = jndiContext.lookup("CustomerHome");
CustomerHome home = (CustomerHome)
PortableRemoteObject.narrow(ref,CustomerHome.class);
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
sdf.setTimeZone(TimeZone.getDefault());
System.out.println("Now : " + sdf.format(cal.getTime()));
String now = sdf.format(cal.getTime());
Date d = sdf.parse(now);
Customer cust_1 = home.create("1234", "Joyce", "Stack", "Joycie Babie", "0879671490", d);
String pk = new String("1234");
Customer cust_2 = home.findByPrimaryKey(pk);
System.out.println("Name: " + cust_2.getFirstName());
System.out.println("LastName: " + cust_2.getLastName());
System.out.println("Contact: "+cust_2.getContactName());
System.out.println("Phone: " +cust_2.getPhoneNumber());
System.out.println("DOB: " +cust_2.getDOB());
}
catch (java.rmi.RemoteException re){re.printStackTrace();}
catch (javax.naming.NamingException ne){ne.printStackTrace();}
catch (javax.ejb.CreateException ce){ce.printStackTrace();}
catch (Exception p){p.printStackTrace();}
}
public static Context getInitialContext()
throws javax.naming.NamingException
{
// return new InitialContext();
//context initialized by jndi.properties file
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
p.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
p.put(Context.PROVIDER_URL, "localhost:1099");
return new javax.naming.InitialContext(p);
}
}
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
<enterprise-beans>
<ejb-name>CustomerEJB</ejb-name>
com.customers.ejb.CustomerHome
com.customers.ejb.Customer
<ejb-class>com.customers.ejb.CustomerBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.String</prim-key-class>
false
<cmp-version>2.x</cmp-version>
<abstract-schema-name>Customer</abstract-schema-name>
<cmp-field><field-name>CustomerID</field-name></cmp-field>
<cmp-field><field-name>FirstName</field-name></cmp-field>
<cmp-field><field-name>LastName</field-name></cmp-field>
<cmp-field><field-name>ContactName</field-name></cmp-field>
<cmp-field><field-name>PhoneNumber</field-name></cmp-field>
<cmp-field><field-name>DOB</field-name></cmp-field>
<primkey-field>CustomerID</primkey-field>
<security-identity><use-caller-identity/></security-identity>
<create-table>false</create-table>
</enterprise-beans>
</ejb-jar>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jbosscmp-jdbc PUBLIC
"-//JBoss//DTD JBOSSCMP-JDBC 3.0//EN"
"http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_3_0.dtd">
<jbosscmp-jdbc>
java:/DefaultDS
<type-mapping>Hypersonic SQL</type-mapping>
true
<enterprise-bean>
<ejb-name>CustomerEJB</ejb-name>
<table-name>Customers</table-name>
<create-table>false</create-table>
<cmp-field>
<field-name>CustomerID</field-name>
<column-name>CustomerID</column-name>
</cmp-field>
<cmp-field>
<field-name>FirstName</field-name>
<column-name>FirstName</column-name>
</cmp-field>
<cmp-field>
<field-name>LastName</field-name>
<column-name>LastName</column-name>
</cmp-field>
<cmp-field>
<field-name>PhoneNumber</field-name>
<column-name>PhoneNumber</column-name>
</cmp-field>
<cmp-field>
<field-name>DOB</field-name>
<column-name>DOB</column-name>
</cmp-field>
</enterprise-bean>
</jbosscmp-jdbc>
<?xml version="1.0" encoding="Cp1252"?>
<enterprise-beans>
<ejb-name>CustomerEJB</ejb-name>
<jndi-name>CustomerHome</jndi-name>
</enterprise-beans>
CREATE TABLE Customers(CustomerID VARCHAR(8), FirstName VARCHAR(20), LastName VARCHAR(20), ContactName VARCHAR(40), PhoneNumber VARCHAR(10), DOB DATE, PRIMARY KEY(CustomerID));
Is this all correct? I have all the XML files in the meta-inf folder and i just jar up the whole lot.