Solved

Configuration Database connecton for CMP

Posted on 2006-11-19
12
399 Views
Last Modified: 2013-11-24
I'm newbie in EJB. Now I'm writing an EJB project. I use JBuilder 2006 and JBoss 4.0 for writing my project with CMP Entity Bean. I try many time but can't connect to database. I use Mysql database, connected by MySql JDBC over DataSource. Pls help me, is there need any configuraton?
0
Comment
Question by:arobe12
  • 6
  • 5
12 Comments
 
LVL 10

Expert Comment

by:mbvvsatish
ID: 17977423
first try deploying this example
http://www.kylev.com/code/jboss-mysql-and-cmp/
0
 
LVL 10

Expert Comment

by:mbvvsatish
ID: 17977429
here is how you set up a mysql datasource in jboss
http://wiki.jboss.org/wiki/Wiki.jsp?page=SetUpAMysqlDatasource
0
 
LVL 5

Expert Comment

by:deeppra
ID: 17977436
send me what error u r getting??
0
 

Author Comment

by:arobe12
ID: 17977480
I can lookup my EJB, but can't connect to database, here is error message that apperes when I run Client test Bean:

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
      java.rmi.ServerException: RuntimeException; nested exception is:
      java.lang.IllegalStateException: createBeanClassInstanceCommand == null
0
 
LVL 10

Expert Comment

by:mbvvsatish
ID: 17977498
looks like a NullPointerException.
can you post the bean code along with you deployment descriptor xml's.
0
 

Author Comment

by:arobe12
ID: 17977514
This is Bean Code

//////////////////////////////////

package testproject;

import java.sql.*;
import java.util.Properties;
import java.util.Enumeration;
import java.util.Vector;
import java.rmi.RemoteException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.ObjectNotFoundException;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;

public class CMPProductBean implements EntityBean {

  EntityContext context;

  public String productId;
  public String productName;
  public String description;
  public double price;

  public String getProductId() {
    System.out.println("getProductId");
    return productId;
  }

  public String getProductName() {
    System.out.println("getProductName");
    return productName;
  }

  public String getDescription() {
    System.out.println("getDescription");
    return description;
  }
  public double getPrice() {
    System.out.println("getPrice");
    return price;
  }

  public String ejbCreate(int productId, String productName,
    String description, double price)
    throws RemoteException, CreateException {
    System.out.println("ejbCreate");
    this.productId = Integer.toString( productId);
    this.productName = productName;
    this.description = description;
    this.price = price;
    return Integer.toString(productId);
  }

  public void ejbPostCreate(int productId, String productName,
    String description, double price)
    throws RemoteException, CreateException {
    System.out.println("ejbPostCreate");
  }

  public void ejbRemove() throws RemoteException {
    System.out.println("ejbRemove");
  }

  public void ejbActivate() {
    System.out.println("ejbActivate");
  }

  public void ejbPassivate() {
    System.out.println("ejbPassivate");
  }

  public void ejbLoad() {
    System.out.println("ejbLoad");
  }
  public void ejbStore() {
    System.out.println("ejbStore");
  }
  public void setEntityContext(EntityContext context) {
    System.out.println("setEntityContext");
    this.context = context;

  }

  public void unsetEntityContext() {
    System.out.println("unsetEntityContext");
    context = null;
  }
}

/////////////////////////////////////////////////////////////////

This is xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd">

<ejb-jar>
  <display-name>HelloCMP</display-name>
  <enterprise-beans>
    <entity>
      <ejb-name>CMPProduct</ejb-name>
      <home>testproject.CMPProductHome</home>
      <remote>testproject.CMPProduct</remote>
      <ejb-class>testproject.CMPProductBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.String</prim-key-class>
      <reentrant>False</reentrant>
      <cmp-field>
        <field-name>productId</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>productName</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>description</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>price</field-name>
      </cmp-field>
      <primkey-field>productId</primkey-field>
      <resource-ref>
        <res-ref-name>jdbc/DataSource</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
      </resource-ref>
    </entity>
  </enterprise-beans>
  <assembly-descriptor>
    <container-transaction>
      <method>
        <ejb-name>CMPProduct</ejb-name>
        <method-name>*</method-name>
      </method>
      <trans-attribute>Required</trans-attribute>
    </container-transaction>
  </assembly-descriptor>
</ejb-jar>
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 10

Expert Comment

by:mbvvsatish
ID: 17977523
i am looking for jbosscmp-jdbc.xml please post it here
0
 

Author Comment

by:arobe12
ID: 17977558
Here is jbosscmp-jdbc.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jbosscmp-jdbc PUBLIC "-//JBoss//DTD JBOSSCMP-JDBC 4.0//EN" "http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_4_0.dtd">

<jbosscmp-jdbc>
  <defaults>
    <datasource>DataSource</datasource>
    <datasource-mapping>Hypersonic SQL</datasource-mapping>
  </defaults>
  <enterprise-beans>
    <entity>
      <ejb-name>CMPProduct</ejb-name>
      <table-name>product</table-name>
      <cmp-field>
        <field-name>productId</field-name>
        <column-name>PRODUCT_ID</column-name>
      </cmp-field>
      <cmp-field>
        <field-name>productName</field-name>
        <column-name>PRODUCT_NAME</column-name>
      </cmp-field>
      <cmp-field>
        <field-name>description</field-name>
        <column-name>DESCRIPTION</column-name>
      </cmp-field>
      <cmp-field>
        <field-name>price</field-name>
        <column-name>PRICE</column-name>
      </cmp-field>
    </entity>
  </enterprise-beans>
</jbosscmp-jdbc>

0
 
LVL 10

Accepted Solution

by:
mbvvsatish earned 500 total points
ID: 17977627
>> <datasource>DataSource</datasource>
>> <datasource-mapping>Hypersonic SQL</datasource-mapping>

change the above lines in jbosscmp-jdbc.xml to the following

<datasource>java:/DataSource</datasource>
<datasource-mapping>MySql</datasource-mapping>
where DataSource is the name of the datasource that you created for mysql
0
 

Author Comment

by:arobe12
ID: 17977689
Succeful but it appears new error:

javax.ejb.CreateException: Error checking if entity exists:com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'PRODUCT_ID' in 'where clause'
0
 

Author Comment

by:arobe12
ID: 17977700
I see the problem. Thanks for your help.
0
 
LVL 10

Expert Comment

by:mbvvsatish
ID: 17977716
check whether you have declared the 'PRODUCT_ID' as an auto_increment column in the mysql database.
if so you need to change your deployment descriptors.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
table example 4 24
eclipse with Angularjs2.0 3 17
use lov values 2 36
how to install java on RHEL image on EC2 4 7
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now