0 Replies Latest reply on Apr 29, 2009 2:21 AM by Manjesh kumar

    CMP Entity bean is not saving to mysql database

    Manjesh kumar Newbie

      Hi,
      I am trying to create CMP entity bean using JBOSS Server 4 and mysql
      1.CMPRemoteI
      package com.remote.interfaces;

      import java.rmi.RemoteException;

      import javax.ejb.EJBObject;

      public interface CMPRemoteI extends EJBObject {

      public String getKey() throws RemoteException;

      public String getName() throws RemoteException;

      public void setName (String s) throws RemoteException;

      public String getPlace() throws RemoteException;

      public void setPlace(String s) throws RemoteException;

      }

      2. CMPHomeI
      package com.home.interfaces;

      import java.rmi.RemoteException;

      import javax.ejb.CreateException;
      import javax.ejb.EJBHome;
      import javax.ejb.FinderException;
      import com.remote.interfaces.CMPRemoteI;

      public interface CMPHomeI extends EJBHome {

      public CMPRemoteI create(String a,String b, String c) throws RemoteException, CreateException;

      public CMPRemoteI findByPrimaryKey (String a) throws RemoteException, FinderException;
      }

      3. ejb-jar.xml in META-INF folder
      <?xml version="1.0" encoding="UTF-8"?>
      <ejb-jar id="ejb-jar_ID" version="2.1"
      xmlns="http://java.sun.com/xml/ns/j2ee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
      <display-name>EJB2App</display-name>
      <enterprise-beans>

      <!-- CMP Entity Bean Mapping Start -->

      <ejb-name>EntityBeanCMP</ejb-name>
      com.home.interfaces.CMPHomeI
      com.remote.interfaces.CMPRemoteI
      <ejb-class>com.beans.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>table1</abstract-schema-name>
      <cmp-field>
      <field-name>key</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>name</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>place</field-name>
      </cmp-field>
      <primkey-field>key</primkey-field>


      <!-- CMP Entity Bean Mapping End -->

      </enterprise-beans>


      <!-- Assembly-Descriptor Mapping Start -->
      <assembly-descriptor>
      <container-transaction>

      <ejb-name>EntityBeanCMP</ejb-name>
      <method-name>*</method-name>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      </assembly-descriptor>

      <!-- Assembly-Descriptor Mapping End -->

      </ejb-jar>

      4. jboss.xml in META-INF folder
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE jboss PUBLIC
      "-//JBoss//DTD JBOSS 3.0//EN"
      "http://www.jboss.org/j2ee/dtd/jboss_3_0.dtd">

      <enterprise-beans>

      <!-- CMP EntityBean Mapping -->

      <ejb-name>EntityBeanCMP</ejb-name>
      <jndi-name>EntityBean/CMP</jndi-name>

      <!-- End -->
      </enterprise-beans>
      <resource-managers />


      5. jbosscmp-jdbc.xml in META-INF folder
      <?xml version="1.0" encoding="UTF-8"?>

      <!DOCTYPE jbosscmp-jdbc>

      <jbosscmp-jdbc>



      java:/DefaultDS

      <!--<datasource-mapping>mysql</datasource-mapping>

      --><create-table>true</create-table>

      <remove-table>false</remove-table>

      <pk-constraint>true</pk-constraint>

      <preferred-relation-mapping>

      foreign-key

      </preferred-relation-mapping>



      <enterprise-beans>



      <ejb-name>EntityBeanCMP</ejb-name>

      <table-name>table1</table-name>

      <cmp-field>

      <field-name>key</field-name>

      <column-name>key</column-name>

      </cmp-field>

      <cmp-field>

      <field-name>name</field-name>

      <column-name>name</column-name>

      </cmp-field>

      <cmp-field>

      <field-name>place</field-name>

      <column-name>place</column-name>

      </cmp-field>



      </enterprise-beans>


      </jbosscmp-jdbc>

      6. mysql-ds.xml in D:\Installations\jboss-4.0.3SP1\server\default\deploy location
      <?xml version="1.0" encoding="UTF-8"?>
      <!-- ========================================================== -->
      <!-- -->
      <!-- JBoss Server Configuration -->
      <!-- -->
      <!-- ========================================================== -->
      <!-- $Id: mysql-ds.xml,v 1.1 2002/07/22 22:57:24 d_jencks Exp $ -->
      <!-- ========================================================== -->
      <!-- Datasource config for MySQL using 2.0.11 driver -->
      <!-- ========================================================== -->

      <local-tx-datasource>
      <jndi-name>MySqlDS</jndi-name>
      <connection-url>jdbc:mysql://localhost/test
      </connection-url>
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <user-name>root</user-name>
      mysql
      </local-tx-datasource>


      7. CallCMPEntityBean client file
      package com.clients;

      import java.util.Properties;
      import java.util.ResourceBundle;

      import javax.naming.Context;
      import javax.naming.InitialContext;

      import com.home.interfaces.CMPHomeI;
      import com.remote.interfaces.CMPRemoteI;
      import com.statics.StaticsClass;

      public class CallCMPEntityBean {

      /**
      * @param args
      */
      public static void main(String[] args) {

      try{
      Properties props = new Properties();

      ResourceBundle rb = ResourceBundle
      .getBundle(StaticsClass.COM_PROPERTIES_MESSAGERESOURCE);
      String contextProviderUrl = rb.getString(StaticsClass.PROVIDER_URL);
      String contextInitialFactory = rb
      .getString(StaticsClass.INITIAL_CONTEXT_FACTORY);

      if (contextProviderUrl != null && contextInitialFactory != null) {

      props.put(Context.PROVIDER_URL, contextProviderUrl);
      props.put(Context.INITIAL_CONTEXT_FACTORY,
      contextInitialFactory);
      props.put(Context.URL_PKG_PREFIXES,
      "org.jboss.naming:org.jnp.interfaces");

      System.out.println("Properties ok");

      } else {
      System.out.println("Context PROVER_URL =" + contextProviderUrl
      + " Context INITIAL_FACTORY =" + contextInitialFactory);
      }

      Context ctx = new InitialContext(props);
      CMPHomeI home = (CMPHomeI) ctx.lookup("EntityBean/CMP");
      System.out.println("home located");

      System.out.println("Add record");
      home.create("4","Manjesh","Kumar");
      System.out.println("record created");

      System.out.println("Find record");
      CMPRemoteI remote= home.findByPrimaryKey("3");
      System.out.println("Name:..."+remote.getName());
      System.out.println("Place:..."+remote.getPlace());

      /*
      System.out.println("Update record");

      System.out.println("update name");
      remote.setName("Manoj");
      String name = remote.getName();
      System.out.println("Name updated");
      System.out.println(name);

      System.out.println("update place");
      remote.setPlace("Lucknow");
      String place=remote.getPlace();
      System.out.println("place updated");
      System.out.println(place);*/

      /*System.out.println("Delete record");
      remote.remove();
      System.out.println("record removed"); */



      }catch(Exception e1){
      System.out.println(" "+e1);
      }

      }

      }


      when i am running the CallCMPEntityBean file the code is executing properly without any exception or error but data is not populated to "test" database.
      actually the basic problem is that table is not creating in test database.

      Kindly help me and let me know where i did mistake.
      please provide the solution.

      Manjesh K