[jboss-user] [Beginners Corner] - CMP not inserting data nor its showing any error.

samcode3 do-not-reply at jboss.com
Wed May 2 14:54:03 EDT 2007


Hi to all,
First of all i would thankx in advance for any help or support provided.
I am not able to insert data in mysql database using CMP. Its getting deployed without any error. When i run the client its displaying no errors. Am not able to fix this since am getting no errors. Please help me. Have been trying from 4 days. Please let me know where i have made an mistake. The following is my code.
----------------------------------------------------------------------------------
Remote Interface:

package cmp.interfaces;
import javax.ejb.EJBObject;
import java.rmi.*;
public interface CMPRemote extends EJBObject {
		public String getEmployeeName() throws RemoteException;
		
		public String getEmpname() throws RemoteException;
		public void setEmpname(String empname) throws RemoteException;
		public String getEmpdept() throws RemoteException;
		public void setEmpdept(String empdept) throws RemoteException;
}

----------------------------------------------------------------------------------
HomeInterface:

package cmp.interfaces;
import javax.ejb.EJBHome;
import java.rmi.*;
import javax.ejb.*;
public interface CMPHome extends EJBHome {
	public CMPRemote create(String empName, String empDept) throws RemoteException, CreateException;
	public CMPRemote findByPrimaryKey(java.lang.String empName) throws FinderException, RemoteException;
}

----------------------------------------------------------------------------------

PrimaryKey Class:

package cmp.classes;
import java.io.*;
public class CMPClassPK implements java.io.Serializable{
	public String name;
	public CMPClassPK(String name){
		this.name=name;
	}	
	public CMPClassPK(){
	
	}
	public String toString(){
		return name.toString();
	}
}

----------------------------------------------------------------------------------

Bean Class:

package cmp.classes;
import javax.ejb.*;
import java.rmi.RemoteException;
import javax.ejb.*;
import javax.naming.*;
import java.sql.*;
import javax.sql.*;
import java.util.*;

abstract public class CMPBean implements EntityBean {
	private EntityContext ctx;
	public CMPBean(){
	
	}
	
	public void setEntityContext(EntityContext ctx){
		this.ctx=ctx;
	}	
	
	public void unsetEntityContext(){
		this.ctx=null;
	}
	
	abstract public String getEmpname();
	abstract public void setEmpname(String empname);
	
	abstract public String getEmpdept();
	abstract public void setEmpdept(String empdept);
	
	public void ejbLoad() throws EJBException, RemoteException {
		// TODO Auto-generated method stub
	}
    
    	public void ejbStore() throws EJBException, RemoteException{
    		// TODO Auto-generated method stub
   	 }
    
    	public void ejbActivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	public void ejbPassivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	public void ejbRemove() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}
	
	public String ejbCreate(String empname, String empdept)throws RemoteException, CreateException{
		setEmpname(empname);
		setEmpdept(empdept);	
		
		return null;
	}
	
	public void ejbPostCreate(String empname, String empdept){
		
	}
	
	public String getEmployeeName(){
		return getEmpname()+"";	
	}
}

----------------------------------------------------------------------------------

Client Class:

package cmp.client;
import java.util.*;
import javax.naming.*;
import javax.rmi.*;
import java.rmi.*;
import javax.ejb.*;
import cmp.interfaces.*;
import cmp.classes.*;

public class Client {
	public static void main(String arg[]){
		Properties props = new Properties();
        try{
		props.put(Context.INITIAL_CONTEXT_FACTORY,
            "org.jnp.interfaces.NamingContextFactory");
        props.put(Context.PROVIDER_URL, "localhost:1099");
        Context ctx = new InitialContext(props);
        CMPHome home = (CMPHome)ctx.lookup("cmp/Bean");
        CMPRemote bean1 = home.create("d", "ddd");
        System.out.println("Value received : " + bean1.getEmployeeName());
        bean1.remove();
        ctx.close();
        }
        catch(NamingException ne){ System.out.println("Error ne: " + ne); }
        catch(RemoteException re){ System.out.println("Error re: " + re); }
        catch(CreateException ce){ System.out.println("Error ce: " + ce); }
        catch(Exception e){
        	System.out.println("Error : " + e);
        }
    }
}

----------------------------------------------------------------------------------

JBoss.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 3.2//EN" "http://www.jboss.org/j2ee/dtd/jboss_3_2.dtd">

<enterprise-beans>
	
		<ejb-name>CMPBean</ejb-name>
		<jndi-name>cmp/Bean</jndi-name>
  		
	
</enterprise-beans>	
<resource-managers>
</resource-managers>


----------------------------------------------------------------------------------

ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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>CMPBean</ejb-name>
			cmp.interfaces.CMPHome
			cmp.interfaces.CMPRemote
			<ejb-class>cmp.classes.CMPBean</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>CMPBean</abstract-schema-name>
			
			<cmp-field>
				<field-name>empname</field-name>
			</cmp-field>
			<cmp-field>
				<field-name>empdept</field-name>
			</cmp-field>
			<primkey-field>empname</primkey-field>
		
	</enterprise-beans>
	<assembly-descriptor>
	</assembly-descriptor>
</ejb-jar>

----------------------------------------------------------------------------------
jbosscmp-jdbc.xml

<?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>
   
      java:/DefaultDS
      <datasource-mapping>mySQL</datasource-mapping>
      <create-table>true</create-table>
      <remove-table>true</remove-table>
      <pk-constraint>true</pk-constraint>
      <preferred-relation-mapping>foreign-key</preferred-relation-mapping>
   
   <enterprise-beans>
   
      
         <ejb-name>CMPBean</ejb-name>
         <table-name>employee</table-name>
         <cmp-field>
            <field-name>empname</field-name>
            <column-name>emp_name</column-name>
            <not-null/>
         </cmp-field>
         <cmp-field>
            <field-name>empdept</field-name>
            <column-name>emp_dept</column-name>
            
         </cmp-field>
      
   </enterprise-beans>
</jbosscmp-jdbc>

----------------------------------------------------------------------------------

mysql-ds.xml

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

  <local-tx-datasource>
    <jndi-name>DefaultDS</jndi-name>
    <connection-url>jdbc:mysql://localhost:3306/test</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>admin</user-name>
    adminpwd
		
	
		<type-mapping>mySQL</type-mapping>
	
</local-tx-datasource>


----------------------------------------------------------------------------------

Database

create table employee(
	emp_name varchar(20) not null,
	emp_dept varchar(20),
	primary key(emp_name)
);

----------------------------------------------------------------------------------
Output when deployed on JBoss3.2.6

23:34:07,359 INFO  [EjbModule] Deploying CMPBean
23:34:07,453 INFO  [EJBDeployer] Deployed: file:/J:/Kits/jboss-3.2.6/server/default/deploy/CMPBean.jar


Please let me know if am making any mistake.

Regards
-sam



View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4042562#4042562

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4042562



More information about the jboss-user mailing list