File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes create method not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "create method not working" Watch "create method not working" New topic
Author

create method not working

Rick Delpo
Greenhorn

Joined: Sep 26, 2006
Posts: 3
Hi,
I have a very simple EJB in Jboss 3.25, windows xp, jdk 5.0, mysql connector J

with files

ProductHomeRemote.java
ProductRemote.java
ProductBean.java
TestClient.java
jbosscmp-jdbc.xml
ejb-jar.xml
jboss.xml


All my code compiles fine and the connection is container managed. I am new to EJB. When I start the server there are no runtime errors and the container creates the table in the database so the connection is ok. When I invoke my test client, my one and only create method gets the context and a reference to the bean but no insert occurs in the table. Am I missing something in my xml files?


code is as follows:

1)

package com.javasrc.licenseserver.ejb.product;

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;

public interface ProductHomeRemote extends javax.ejb.EJBHome
{
public ProductRemote create( Integer id, String name ) throws CreateException,

RemoteException;
public ProductRemote findByPrimaryKey( Integer id ) throws FinderException,

RemoteException;
}

2)

package com.javasrc.licenseserver.ejb.product;

import java.rmi.RemoteException;

public interface ProductRemote extends javax.ejb.EJBObject
{
public String getName() throws RemoteException;
public void setName( String name ) throws RemoteException;
}


3)

package com.javasrc.licenseserver.ejb.product;

import javax.ejb.*;
import java.util.*;

public abstract class ProductBean implements EntityBean
{
public Integer ejbCreate( Integer id, String name ) throws CreateException
{
setId(id);
setName(name);
return null;
}

public void ejbPostCreate( Integer id, String name )
{
}

public abstract Integer getId();
public abstract void setId( Integer id );

public abstract String getName();
public abstract void setName( String name );

public void setEntityContext( EntityContext ctx ) {}
public void unsetEntityContext() {}
public void ejbLoad() {}
public void ejbStore() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbRemove() {}
}


4) very simple servlet test client..a welcome file called home.html accesses this

test client

package com.javasrc.licenseserver.client;

import java.io.IOException;
import java.util.Hashtable;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.*;
import com.javasrc.licenseserver.ejb.product.ProductHomeRemote;
import com.javasrc.licenseserver.ejb.product.ProductRemote;

public class TestClient extends HttpServlet {


public void init() throws ServletException {

}

public void service(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException {

ServletContext sc;
RequestDispatcher rd;
String title = "Servlet interface to EJB";

try {

InitialContext jndiContext = new InitialContext();
System.out.println("Got context");
Object ref = jndiContext.lookup("ProductHomeRemote");
System.out.println("Got reference");
ProductHomeRemote home = (ProductHomeRemote) PortableRemoteObject.narrow(ref,

ProductHomeRemote.class);

//insert a new product

Integer primaryKey =new Integer(25);
ProductRemote product = home.create(primaryKey, "sample entry");
}
catch (Exception ex) {
ex.printStackTrace();
}

}

}


5)

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE jbosscmp-jdbc PUBLIC "-//JBoss//DTD JBOSSCMP-JDBC 3.2//EN"

"http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_3_2.dtd">

<jbosscmp-jdbc>
<defaults>
<datasource>java:/MySqlDS</datasource>
<datasource-mapping>mySQL</datasource-mapping>
<create-table>true</create-table>
<remove-table>false</remove-table>
</defaults>

<enterprise-beans>
<entity>
<ejb-name>ProductEJB</ejb-name>
<table-name>Product</table-name>
<cmp-field>
<field-name>id</field-name>
<column-name>product_id</column-name>
</cmp-field>
<cmp-field>
<field-name>name</field-name>
<column-name>product_name</column-name>
</cmp-field>
</entity>
</enterprise-beans>
</jbosscmp-jdbc>


6)

<!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>
<entity>
<ejb-name>ProductEJB</ejb-name>
<home>com.javasrc.licenseserver.ejb.product.ProductHomeRemote</home>
<remote>com.javasrc.licenseserver.ejb.product.ProductRemote</remote>
<ejb-class>com.javasrc.licenseserver.ejb.product.ProductBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>Product</abstract-schema-name>
<cmp-field><field-name>id</field-name></cmp-field>
<cmp-field><field-name>name</field-name></cmp-field>
<primkey-field>id</primkey-field>
<security-identity><use-caller-identity/></security-identity>
</entity>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>ProductEJB</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>


7)

<?xml version="1.0"?>
<jboss>
<enterprise-beans>
<entity>
<ejb-name>ProductEJB</ejb-name>
<jndi-name>ProductHomeRemote</jndi-name>
</entity>
</enterprise-beans>
</jboss>
Rick Delpo
Greenhorn

Joined: Sep 26, 2006
Posts: 3
I figured this out on my own. All my code is correct, I forgot to put jbossall-client.jar in the lib folder of my test client app.

Now this leads to another issue, the insert worked and is now in the mysql table but when I tried to recompile my test client with new input parameters I got this runtime exception in jboss:

19:40:12,250 INFO [STDOUT] javax.ejb.DuplicateKeyException: Entity with primary
key 25 already exists

19:45:41,703 INFO [STDOUT] at org.jboss.ejb.plugins.cmp.jdbc.JDBCInsertPKCr
eateCommand.beforeInsert(JDBCInsertPKCreateCommand.java:86)
19:45:41,703 INFO [STDOUT] at org.jboss.ejb.plugins.cmp.jdbc.JDBCAbstractCr
eateCommand.execute(JDBCAbstractCreateCommand.java:136)
19:45:41,703 INFO [STDOUT] at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManag
er.createEntity(JDBCStoreManager.java:562)
19:45:41,703 INFO [STDOUT] at org.jboss.ejb.plugins.CMPPersistenceManager.c
reateEntity(CMPPersistenceManager.java:203)
19:45:41,703 INFO [STDOUT] at org.jboss.resource.connectionmanager.CachedCo
nnectionInterceptor.createEntity(CachedConnectionInterceptor.java:269)
19:45:41,703 INFO [STDOUT] at org.jboss.ejb.EntityContainer.createHome(Enti
tyContainer.java:725)

.......more but not to bore you with it ..............


The server thinks I still have the same data. I even tried rebooting and going back to make sure I had a NEW test client class file and it still returned the same info.

Thanks in advance for helping a newbie
Rick Delpo
Greenhorn

Joined: Sep 26, 2006
Posts: 3
sorry to bother everyone.

just figured it out again.

my test client class was in both the server jar file and the client jar file. I took it out at the server level.

apparently cmp was looking at the server jar first and seeing my test client class there, the one I did not change, and of course it showed a duplicate primary key.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: create method not working