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 EJB Entity Bean is created but unable to insert data into database 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 "EJB Entity Bean is created but unable to insert data into database" Watch "EJB Entity Bean is created but unable to insert data into database" New topic
Author

EJB Entity Bean is created but unable to insert data into database

Subhrangshu Samanta
Greenhorn

Joined: Jul 30, 2012
Posts: 14
Hi All,

I have successfully created EJB Entity Bean using xdoclet(1.2.3) and JBOSS(5.1.0 GA). Please find the ejbCreate function below :

public java.math.BigDecimal ejbCreate(java.math.BigDecimal id, java.lang.String name, java.lang.String address, java.lang.String company) throws javax.ejb.CreateException {
// EJB 2.0 spec says return null for CMP ejbCreate methods.
// TODO: YOU MUST INITIALIZE THE FIELDS FOR THE BEAN HERE.
// setMyField("Something");
// begin-user-code
setId(id);
setName(name);
setAddress(address);
setCompany(company);
return null;
// end-user-code
}

From the client end I have called the create method in the Bean in the below manner:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter pw = response.getWriter();

try
{
Integer t = 2;
String str = t.toString();
BigDecimal bd = new BigDecimal(str);
entiHome h = entiUtil.getHome();
enti ben = h.create(bd, "George", "USA", "sweden");
pw.println("Bean Created.....!!!");
pw.println("Name : "+ben.getName());
pw.println("Address : "+ben.getAddress());
pw.println("Company : "+ben.getCompany());
}
catch(Exception e)
{
System.out.println("The error is : "+e.toString());
}
}

The bean is created but there is no data in the database.

I have attached the structure of the files in Eclipse[color=blue]
. Can anyone help me in this issue..........??.......it has become a nightmare..............!!!

please allow me to know if any more details is required.....
[/color]


[Thumbnail for Entity_Bean_Structure.jpg]

Manjunath Gajula
Greenhorn

Joined: Dec 21, 2011
Posts: 12
Can you share your java doc for ejbCreate() method?

It should be something like this.
/**
*
* @param
* @param
* @param
* @param
* @return
*
* @ejb.create-method
* @throws javax.ejb.CreateException
*/
Manjunath Gajula
Greenhorn

Joined: Dec 21, 2011
Posts: 12
Also, i assume that you are passing attributes properly. BigDecimal, String, String, String.

And i hope you have ejbPostCreate() method.


public void ejbPostCreate(java.math.BigDecimal id, java.lang.String name, java.lang.String address, java.lang.String company) throws javax.ejb.CreateException {
}


The container invokes this method immediately after it calls ejbCreate.
Subhrangshu Samanta
Greenhorn

Joined: Jul 30, 2012
Posts: 14
Yes...I agree with you about ejbPostCreate. Please find the details of ejbCreate and ejbPostCreate method below:-

/**
*
* <!-- begin-user-doc -->
* The ejbCreate method.
* <!-- end-user-doc -->
*
* <!-- begin-xdoclet-definition -->
* @ejb.create-method
* <!-- end-xdoclet-definition -->
* @generated
*/
public java.math.BigDecimal ejbCreate(java.math.BigDecimal id, java.lang.String name, java.lang.String address, java.lang.String company) throws javax.ejb.CreateException {
// EJB 2.0 spec says return null for CMP ejbCreate methods.
// TODO: YOU MUST INITIALIZE THE FIELDS FOR THE BEAN HERE.
// setMyField("Something");
// begin-user-code
setId(id);
setName(name);
setAddress(address);
setCompany(company);
return null;
// end-user-code
}



/**
* <!-- begin-user-doc -->
* The container invokes this method immediately after it calls ejbCreate.
* <!-- end-user-doc -->
*
* @generated
*/
public void ejbPostCreate(java.math.BigDecimal id, java.lang.String name, java.lang.String address, java.lang.String company) throws javax.ejb.CreateException {
// begin-user-code
// end-user-code
}

Can you please help me to know what I should need to do in order to insert data into the database....(The Bean has been generated successfully but there is no data in the database)....

.....I am using JBOSS server.....my ejb-jar.xml is given below.....please allow me to know if I need to modify it....or need to do something more to insert data in the database...........



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

<ejb-jar id="ejb-jar_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" version="2.1">

<description><![CDATA[EntityEJB generated by eclipse wtp xdoclet extension.]]></description>
<display-name>EntityEJB</display-name>

<enterprise-beans>

<!-- Session Beans -->
<!--
To add session beans that you have deployment descriptor info for, add
a file to your XDoclet merge directory called session-beans.xml that contains
the <session></session> markup for those beans.
-->

<!-- Entity Beans -->
<entity id="ContainerManagedEntity_enti">
<description><![CDATA[<!-- begin-xdoclet-definition -->]]></description>

<ejb-name>enti</ejb-name>

<home>entpkg.entiHome</home>
<remote>entpkg.enti</remote>
<local-home>entpkg.entiLocalHome</local-home>
<local>entpkg.entiLocal</local>

<ejb-class>entpkg.entiCMP</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.math.BigDecimal</prim-key-class>
<reentrant>false</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>entiSCHEMA</abstract-schema-name>
<cmp-field id="CMPAttribute_1">
<description><![CDATA[<!-- begin-user-doc --> CMP Field id Returns the id]]></description>
<field-name>id</field-name>
</cmp-field>
<cmp-field id="CMPAttribute_2">
<description><![CDATA[<!-- begin-user-doc --> CMP Field name Returns the name]]></description>
<field-name>name</field-name>
</cmp-field>
<cmp-field id="CMPAttribute_3">
<description><![CDATA[<!-- begin-user-doc --> CMP Field address Returns the address]]></description>
<field-name>address</field-name>
</cmp-field>
<cmp-field id="CMPAttribute_4">
<description><![CDATA[<!-- begin-user-doc --> CMP Field company Returns the company]]></description>
<field-name>company</field-name>
</cmp-field>
<primkey-field>id</primkey-field>

<query>
<query-method>
<method-name>findAll</method-name>
<method-params>
</method-params>
</query-method>
<ejb-ql><![CDATA[SELECT OBJECT(a) FROM entiSCHEMA as a]]></ejb-ql>
</query>
<!-- Write a file named ejb-finders-MyBean.xml if you want to define extra finders. -->

</entity>

<!--
To add entity beans that you have deployment descriptor info for, add
a file to your XDoclet merge directory called entity-beans.xml that contains
the <entity></entity> markup for those beans.
-->

<!-- Message Driven Beans -->
<!--
To add message driven beans that you have deployment descriptor info for, add
a file to your XDoclet merge directory called message-driven-beans.xml that contains
the <message-driven></message-driven> markup for those beans.
-->

</enterprise-beans>

<!-- Relationships -->

<!-- Assembly Descriptor -->
<!--
To specify your own assembly descriptor info here, add a file to your
XDoclet merge directory called assembly-descriptor.xml that contains
the <assembly-descriptor></assembly-descriptor> markup.
-->

<assembly-descriptor id="AssemblyDescriptor_1">
<!--
To specify additional security-role elements, add a file in the merge
directory called ejb-security-roles.xml that contains them.
-->

<!-- method permissions -->
<!--
To specify additional method-permission elements, add a file in the merge
directory called ejb-method-permissions.ent that contains them.
-->

<!-- transactions -->
<!--
To specify additional container-transaction elements, add a file in the merge
directory called ejb-container-transactions.ent that contains them.
-->

<!-- finder transactions -->

<!-- message destinations -->
<!--
To specify additional message-destination elements, add a file in the merge
directory called ejb-message-destinations.ent that contains them.
-->

<!-- exclude list -->
<!--
To specify an exclude-list element, add a file in the merge directory
called ejb-exclude-list.xml that contains it.
-->
</assembly-descriptor>

</ejb-jar>




Thank You.....
Manjunath Gajula
Greenhorn

Joined: Dec 21, 2011
Posts: 12
Can you let me know what piece of code is there in your bean class?
Subhrangshu Samanta
Greenhorn

Joined: Jul 30, 2012
Posts: 14
Hi,

Please find the code in my Bean class below:---



/**
*
*/
package entpkg;

import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;
import java.util.Collection;





/**
* <!-- begin-xdoclet-definition -->
* @ejb.bean name="enti"
* jndi-name="enti"
* type="CMP"
* primkey-field="id"
* schema="entiSCHEMA"
* cmp-version="2.x"
*
* @ejb.persistence
* table-name="JAVAMAN.TESTING"
*
* @ejb.finder
* query="SELECT OBJECT(a) FROM entiSCHEMA as a"
* signature="java.util.Collection findAll()"
*
* @ejb.pk class="java.math.BigDecimal"
*
*
* <!-- end-xdoclet-definition -->
* @generated
**/

public abstract class MyBean implements javax.ejb.EntityBean {

/**
*
* <!-- begin-user-doc -->
* The ejbCreate method.
* <!-- end-user-doc -->
*
* <!-- begin-xdoclet-definition -->
* @ejb.create-method
* <!-- end-xdoclet-definition -->
* @generated
*/
public java.math.BigDecimal ejbCreate(java.math.BigDecimal id, java.lang.String name, java.lang.String address, java.lang.String company) throws javax.ejb.CreateException {
// EJB 2.0 spec says return null for CMP ejbCreate methods.
// TODO: YOU MUST INITIALIZE THE FIELDS FOR THE BEAN HERE.
// setMyField("Something");
// begin-user-code
setId(id);
setName(name);
setAddress(address);
setCompany(company);
return null;
// end-user-code
}

/**
* <!-- begin-user-doc -->
* The container invokes this method immediately after it calls ejbCreate.
* <!-- end-user-doc -->
*
* @generated
*/
public void ejbPostCreate(java.math.BigDecimal id, java.lang.String name, java.lang.String address, java.lang.String company) throws javax.ejb.CreateException {
// begin-user-code
// end-user-code
}

/**
*
*
* <!-- begin-user-doc -->
* CMP Field id
*
* Returns the id
* @return the id
*
* <!-- end-user-doc -->
*
* <!-- begin-xdoclet-definition -->
*
* @ejb.persistent-field
* @ejb.persistence
* column-name="ID"
* jdbc-type="INTEGER"
* sql-type="NUMBER"
* read-only="false"
* @ejb.pk-field
*
* @ejb.interface-method
*
* <!-- end-xdoclet-definition -->
* @generated
*/
public abstract java.math.BigDecimal getId();

/**
* <!-- begin-user-doc -->
* Sets the id
*
* @param java.math.BigDecimal the new id value
* <!-- end-user-doc -->
*
* <!-- begin-xdoclet-definition -->
* @ejb.interface-method
* <!-- end-xdoclet-definition -->
* @generated
*/
public abstract void setId(java.math.BigDecimal id);

/**
*
*
* <!-- begin-user-doc -->
* CMP Field name
*
* Returns the name
* @return the name
*
* <!-- end-user-doc -->
*
* <!-- begin-xdoclet-definition -->
*
* @ejb.persistent-field
* @ejb.persistence
* column-name="NAME"
* jdbc-type="VARCHAR"
* sql-type="VARCHAR2"
* read-only="false"
*
*
* @ejb.interface-method
*
* <!-- end-xdoclet-definition -->
* @generated
*/
public abstract java.lang.String getName();

/**
* <!-- begin-user-doc -->
* Sets the name
*
* @param java.lang.String the new name value
* <!-- end-user-doc -->
*
* <!-- begin-xdoclet-definition -->
* @ejb.interface-method
* <!-- end-xdoclet-definition -->
* @generated
*/
public abstract void setName(java.lang.String name);

/**
*
*
* <!-- begin-user-doc -->
* CMP Field address
*
* Returns the address
* @return the address
*
* <!-- end-user-doc -->
*
* <!-- begin-xdoclet-definition -->
*
* @ejb.persistent-field
* @ejb.persistence
* column-name="ADDRESS"
* jdbc-type="VARCHAR"
* sql-type="VARCHAR2"
* read-only="false"
*
*
* @ejb.interface-method
*
* <!-- end-xdoclet-definition -->
* @generated
*/
public abstract java.lang.String getAddress();

/**
* <!-- begin-user-doc -->
* Sets the address
*
* @param java.lang.String the new address value
* <!-- end-user-doc -->
*
* <!-- begin-xdoclet-definition -->
* @ejb.interface-method
* <!-- end-xdoclet-definition -->
* @generated
*/
public abstract void setAddress(java.lang.String address);

/**
*
*
* <!-- begin-user-doc -->
* CMP Field company
*
* Returns the company
* @return the company
*
* <!-- end-user-doc -->
*
* <!-- begin-xdoclet-definition -->
*
* @ejb.persistent-field
* @ejb.persistence
* column-name="COMPANY"
* jdbc-type="VARCHAR"
* sql-type="VARCHAR2"
* read-only="false"
*
*
* @ejb.interface-method
*
* <!-- end-xdoclet-definition -->
* @generated
*/
public abstract java.lang.String getCompany();

/**
* <!-- begin-user-doc -->
* Sets the company
*
* @param java.lang.String the new company value
* <!-- end-user-doc -->
*
* <!-- begin-xdoclet-definition -->
* @ejb.interface-method
* <!-- end-xdoclet-definition -->
* @generated
*/
public abstract void setCompany(java.lang.String company);

/* (non-Javadoc)
* @see javax.ejb.EntityBean#ejbActivate()
*/

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

}

/* (non-Javadoc)
* @see javax.ejb.EntityBean#ejbLoad()
*/
public void ejbLoad() throws EJBException, RemoteException {
// TODO Auto-generated method stub

}

/* (non-Javadoc)
* @see javax.ejb.EntityBean#ejbPassivate()
*/
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub

}

/* (non-Javadoc)
* @see javax.ejb.EntityBean#ejbRemove()
*/
public void ejbRemove() throws RemoveException, EJBException,
RemoteException {
// TODO Auto-generated method stub

}

/* (non-Javadoc)
* @see javax.ejb.EntityBean#ejbStore()
*/
public void ejbStore() throws EJBException, RemoteException {
// TODO Auto-generated method stub

}

/* (non-Javadoc)
* @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
*/
public void setEntityContext(EntityContext arg0) throws EJBException,
RemoteException {
// TODO Auto-generated method stub

}

/* (non-Javadoc)
* @see javax.ejb.EntityBean#unsetEntityContext()
*/
public void unsetEntityContext() throws EJBException, RemoteException {
// TODO Auto-generated method stub

}

/**
*
*/
public MyBean() {
// TODO Auto-generated constructor stub
}

}



Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB Entity Bean is created but unable to insert data into database