adrian mills

Ranch Hand
+ Follow
since Dec 09, 2004
Merit badge: grant badges
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by adrian mills

Hi All,


I am trying to store an java.lang.Object in MySQL database using CMP 2.0 and JBoss App Server.
There is a xml String in this Object.
When I retreive the Object by using

String obj = new String(resultSet.getBlob(4).getBytes(1,(int)resultSet.getBlob(4).length()));

the above code.
I have some unecessary data at the start inbetween and at the end of the String Object which I saved.
Can anyone tell me why this is happening.
Does anyone have the code to retrieve a blob from MySQL using JDBC?


Thanks for all your help in advance.
Waiting for all your suggestions
With Regards
Adrian
Hi All,

Thanks for any help or suggestion in advance.

I have A form in which there are two buttons in the form of images


<html:image src="images/buttons/submit.gif" alt="" property="method" value="button.addPatientInfo"/>

<html:image src="images/buttons/review.gif" alt="" property="method" value="button.reviewPatientInfo"/>


I want it to function in the following manner.

When a user clicks submit.gif button I want the form to be submitted to a action called SubmitPatientInformation.Here it shud call Add method in the action class.

When the user clicks review I want the form to be submitted to a action called SubmitPatientInformation.Here it shud call a review method in the same action class.


I have extended my action class from DispatchAction.

Please can anyone tell me how this is to be done.

If there is any other way please explain to me.
Some code might help

Thanks all once again
With Regards
Adrian.
17 years ago
Hi Friends,

I want to display a hospital list using struts.

Here is the problem.
I have a hospitaldata value object.
Hospital data contains the following fields

String hospitalname;
String hospitalcity;
Double hospitalid;
Integer zipcode;


I put all the hospitaldata objects in to an arrayList and set it in the request object.

I want to display the hospitalname to the user.
But when the user submits the form the hospitalid should be submitted.

Can anyone tell me how this is done.
I feel logic:iterate can be used.
But it gives problem when displaying Double values.Has anyone solved this problem.

Thanks for your help
With Regards
Adrian
17 years ago
Hi Marc
Thanks for your answers.
I tried both the methods which you told but with no success.
I want to tell you some points.
Actually
HospitalData is a value object with
Long hospitalid
String hospitalname
String hospitalcity


I add mulptile such dataobjects to my arraylist collection(allhospitallist).


So my allhospitallist contains mulitple HospitalData objects.

When I try the following code on my front end.
<html:select property="hospitalId">


<logic:iterate id="hospitalData" name="allhospitallist">
<option value=''><bean:write name="hospitalData" property="hospitalID"/></option>
</logic:iterate>

</html:select>


I get the following exception

javax.servlet.ServletException: Cannot find message resources under key org.apache.struts.action.MESSAGE
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.AddHospitalAdmin_jsp._jspService(org.apache.jsp.AddHospitalAdmin_jsp:239)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)


The case is when I am trying to display the Long value hospitalid on the front end.If I try to acces anyother field from this data object such as hospitalcity etc everything is displayed properly.
Can you tell me how will i get access to the hospital id using the logic:iterate tag.

Action html ptionsCollection cannot be used as there is no label and property.

Both the hospital name field an hospitalid field are independent.

Thanks a lot Marc for your help.Hope u get to know what my real problem is.

Thanks once again.
Regards
Adrian
17 years ago
Hi Friends,

I am using struts 1.2.
I am getting the following problem.
I am trying to iterate through hospitaldata which is stored in request in an ArrayList.

On the JSP side I am using the following code
<td>5 Hospital Name:---</td>
<td>
<select Name="hospitalid">

<logic:iterate id="hosp" name="allhospitallist">
<option value='<bean:write name="hosp" property="hospitalid"/>'><bean:write name="hosp" property="hospitalname"/></option>
</logic:iterate>

</select>
</td>

hospitalData.hospitalid field is a java.lang.Long.
I am getting the following error:

javax.servlet.ServletException: Cannot find message resources under key org.apache.struts.action.MESSAGE
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.CreateUser_jsp._jspService(org.apache.jsp.CreateUser_jsp:244)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)

When I use

<logic:iterate id="hosp" name="allhospitallist">
<option value='<bean:write name="hosp" property="hospitalname"/>'><bean:write name="hosp" property="hospitalname"/></option>
</logic:iterate>

There is no problem.


It seems that it has got to do something with the data type of id which is java.lang.Long.
Can anyone tell me what the problem is???

Thanks for your help in advance
Regards
Adrian
17 years ago
Hi All,

I am using an entity bean to create my table in MySQL4.1.13
I have a table called user which has userid,username,password as its columns.
The userid is a primary key over here.
But I want the user name to be unique.
I.e if a user with the name 'Adrian' is present in the database then another user with this user name should not be created.

I have been told that you can configure a unique key in jbosscmp-jdbc.xml using insert-after-ejb-post-create.

But i dont know how to use it.
Can anyone give me a sample code for using this.

Any help would be appreciated.
Thanks
p2
17 years ago
I have solved the problem.It was in my ejbCreate method.
I have used this.hospitalid but instead I should be using setHospitalid().
Thanks a lot for all your help
17 years ago
Hello Friends,

I deploy Hospital entity bean.The tables are created properly in the database.
While entering a record using the CMP hospital bean I get the following error

13:23:06,031 ERROR [Hospital] Could not create entity
java.sql.SQLException: Column 'hospitalid' cannot be null
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2921)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1570)
at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1085)
at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:670)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1159)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1076)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1061)
at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:316)
at org.jboss.ejb.plugins.cmp.jdbc.JDBCAbstractCreateCommand.executeInsert(JDBCAbstractCreateCommand.java:352)
at org.jboss.ejb.plugins.cmp.jdbc.JDBCAbstractCreateCommand.performInsert(JDBCAbstractCreateCommand.java:308)

Here is the code for my HospitalEJB


public abstract class HospitalEJB implements EntityBean {

private EntityContext context;
public Long hospitalid;
public String hospitalname;

public void ejbLoad() throws EJBException, RemoteException {

}
public void ejbActivate() throws EJBException, RemoteException {

}

public Long ejbCreate(HospitalData hospitalData) throws CreateException {
System.out.println("Came in ejbCreate method of HospitalEJB");
Long myid = new Long(UniqueIdGenerator.getId());
System.out.println("My id generated ====== " + myid);
this.hospitalid = myid;
System.out.println("My id generated ====== " + this.hospitalid);
this.hospitalname = hospitalData.getHospitalname();
System.out.println("Hospital Name " + hospitalData.getHospitalname());
return myid;

}

public void ejbPostCreate(HospitalData hospitalData)
throws CreateException, RemoteException {
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
System.out.println("Came in EJB POSTCreate of HospitalEJB");
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

}

public void ejbRemove()
throws RemoveException, RemoteException, EJBException {

}

public void setEntityContext(EntityContext context)
throws EJBException, RemoteException {

}

public void unsetEntityContext() throws EJBException, RemoteException {

}

public void ejbPassivate() throws EJBException, RemoteException {

}

public void ejbStore() throws EJBException, RemoteException {

}

/**
* @return
*/
public abstract Long getHospitalid();

/**
* @return
*/
public abstract String getHospitalname();

/**
* @param long1
*/
public abstract void setHospitalid(Long long1);

/**
* @param string
*/
public abstract void setHospitalname(String string);

public HospitalData getHospitalData() {
System.out.println("Came in get hospital data");
System.out.println("In Get method of HospitalEJB" + this.hospitalid);
System.out.println("In Get method of HospitalEJB" + this.hospitalname);

HospitalData hospitalData = new HospitalData();
hospitalData.setHospitalid(this.hospitalid);
hospitalData.setHospitalname(this.hospitalname);
System.out.println(
"In Get method of HospitalEJB" + hospitalData.getHospitalid());
System.out.println(
"In Get method of HospitalEJB" + hospitalData.getHospitalname());
return hospitalData;
}

public void setHospitalData(HospitalData hospitalData) {
System.out.println(
"In Set method of HospitalEJB" + hospitalData.getHospitalname());
this.hospitalid = hospitalData.getHospitalid();
this.hospitalname = hospitalData.getHospitalname();
}

}

Here is my 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>
<session>
<ejb-name>UserManager</ejb-name>
<home>com.cygnet.medina.UserManager.UserManagerHome</home>
<remote>com.cygnet.medina.UserManager.UserManager</remote>
<ejb-class>com.cygnet.medina.UserManager.UserManagerEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>

<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>UserManager</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>


<session>
<ejb-name>HospitalManager</ejb-name>
<home>com.cygnet.medina.HospitalManager.HospitalManagerHome</home>
<remote>com.cygnet.medina.HospitalManager.HospitalManager</remote>
<ejb-class>com.cygnet.medina.HospitalManager.HospitalManagerEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>

<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>HospitalManager</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>

<entity>
<ejb-name>AnesthesiaUser</ejb-name>
<home>com.cygnet.medina.user.AnesthesiaUserHome</home>
<remote>com.cygnet.medina.user.AnesthesiaUser</remote>
<ejb-class>com.cygnet.medina.user.AnesthesiaUserEJB</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Long</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>anesthesiauser</abstract-schema-name>
<cmp-field>
<field-name>userid</field-name>
<column-name>userid</column-name>
</cmp-field>
<cmp-field>
<field-name>username</field-name>
<column-name>username</column-name>
</cmp-field>
<cmp-field>
<field-name>password</field-name>
<column-name>password</column-name>
</cmp-field>
<cmp-field>
<field-name>usertype</field-name>
<column-name>usertype</column-name>
</cmp-field>
<cmp-field>
<field-name>createddate</field-name>
<column-name>createddate</column-name>
</cmp-field>
<cmp-field>
<field-name>updateddate</field-name>
<column-name>updateddate</column-name>
</cmp-field>
<primkey-field>userid</primkey-field>
<query>
<query-method>
<method-name>findByUsername</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql>SELECT OBJECT(t) FROM anesthesiauser AS t WHERE t.username = ?1</ejb-ql>
</query>

</entity>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>AnesthesiaUser</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
<!-- Added -->

<entity>
<ejb-name>Hospital</ejb-name>
<home>com.cygnet.medina.Hospital.HospitalHome</home>
<remote>com.cygnet.medina.Hospital.Hospital</remote>
<ejb-class>com.cygnet.medina.Hospital.HospitalEJB</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Long</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>hospital</abstract-schema-name>
<cmp-field>
<field-name>hospitalid</field-name>
</cmp-field>
<cmp-field>
<field-name>hospitalname</field-name>
</cmp-field>
<primkey-field>hospitalid</primkey-field>
</entity>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Hospital</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
<!-- End -->

</enterprise-beans>
</ejb-jar>

Here is my jbosscmp-jdbc.xml

<?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">

<!-- ===================================================================== -->
<!-- -->
<!-- Standard JBossCMP-JDBC Configuration -->
<!-- -->
<!-- ===================================================================== -->

<!-- $Id: standardjbosscmp-jdbc.xml,v 1.84.2.6 2005/01/26 18:51:35 starksm Exp $ -->

<jbosscmp-jdbc>

<defaults>
<datasource>java:/MySqlDS</datasource>
<datasource-mapping>mySQL</datasource-mapping>
<create-table>true</create-table>
<remove-table>true</remove-table>
<read-only>false</read-only>
<read-time-out>300000</read-time-out>
<row-locking>false</row-locking>
<pk-constraint>true</pk-constraint>
<fk-constraint>false</fk-constraint>
</defaults>

<enterprise-beans>
<entity>
<ejb-name>AnesthesiaUser</ejb-name>

<table-name>anesthesiauser</table-name>
</entity>

<!-- Added -->
<entity>
<ejb-name>Hospital</ejb-name>
<table-name>hospital</table-name>
</entity>

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

</jbosscmp-jdbc>


I am using JBoss 4.0.2 and MySQL 4.1.13
17 years ago
Oops the indentation does not remain when I submit my question ken.
I am sorry can u tell me how do we enable this.
I am really sorry to cause this inconvinience to you.
17 years ago
Sorry Ken,
I am resubmitting the code I hope this is formatted properly.


This is my HospitalManagerEJB used to look up the Hospital EJB.


package com.cygnet.medina.HospitalManager;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.cygnet.medina.Hospital.Hospital;
import com.cygnet.medina.Hospital.HospitalData;
import com.cygnet.medina.Hospital.HospitalHome;
import com.cygnet.medina.user.UserData;
import com.cygnet.medina.util.JNDINames;


public class HospitalManagerEJB implements SessionBean {

private SessionContext ctx;

public void ejbCreate() {
//do nothing
}

public void ejbRemove() {
}

public void ejbActivate() {
}

public void ejbPassivate() {
}

public void setSessionContext(SessionContext sessionContext) {
ctx = sessionContext;
}

public HospitalData addHospital(HospitalData hospitalData)
throws EJBException, RemoteException {
HospitalData newHospitalData = new HospitalData();

try {
System.out.println("Debug in HospitalManager 1********");
InitialContext initialContext = new InitialContext();
System.out.println("Debug in HospitalManager 2********");
HospitalHome hospitalHome =
(HospitalHome) initialContext.lookup(JNDINames.Hospital_Home);
System.out.println("Debug in HospitalManager 3********");
Hospital hospital = hospitalHome.create(hospitalData);
System.out.println("$$$$$$$Created Hospital$$$$$$$$$$$");
System.out.println(
"Hospital Primary Key" + hospital.getPrimaryKey().toString());
newHospitalData = hospital.getHospitalData();

return newHospitalData;
} catch (NamingException ne) {
throw new EJBException(ne.getMessage());
} catch (CreateException ce) {
throw new EJBException(ce.getMessage());
}
}

}


This is my HospitalEJB.java used to enter data in the MySQL database

package com.cygnet.medina.Hospital;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;
import com.cygnet.medina.util.UniqueIdGenerator;

public abstract class HospitalEJB implements EntityBean {

private EntityContext context;
public Long hospitalid;
public String hospitalname;

public void ejbLoad() throws EJBException, RemoteException {

}
public void ejbActivate() throws EJBException, RemoteException {

}

public Long ejbCreate(HospitalData hospitalData) throws CreateException {
Long myid = new Long(UniqueIdGenerator.getId());
System.out.println("My id generated ====== " + myid);
this.hospitalid = myid;
System.out.println("My id generated ====== " + this.hospitalid);
System.out.println("Came in ejbCreate method of HospitalEJB");

this.hospitalname = hospitalData.getHospitalname();
return null;

}

public void ejbPostCreate(HospitalData hospitalData)
throws CreateException, RemoteException {
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
System.out.println("Came in EJB POSTCreate of HospitalEJB");
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

}

public void ejbRemove()
throws RemoveException, RemoteException, EJBException {

}

public void setEntityContext(EntityContext context)
throws EJBException, RemoteException {

}

public void unsetEntityContext() throws EJBException, RemoteException {

}

public void ejbPassivate() throws EJBException, RemoteException {

}

public void ejbStore() throws EJBException, RemoteException {

}

/**
* @return
*/
public abstract Long getHospitalid();

/**
* @return
*/
public abstract String getHospitalname();

/**
* @param long1
*/
public abstract void setHospitalid(Long long1);

/**
* @param string
*/
public abstract void setHospitalname(String string);

public HospitalData getHospitalData() {
System.out.println("Came in get hospital data");
System.out.println("In Get method of HospitalEJB" + this.hospitalid);
System.out.println("In Get method of HospitalEJB" + this.hospitalname);

HospitalData hospitalData = new HospitalData();
hospitalData.setHospitalid(this.hospitalid);
hospitalData.setHospitalname(this.hospitalname);
System.out.println(
"In Get method of HospitalEJB" + hospitalData.getHospitalid());
System.out.println(
"In Get method of HospitalEJB" + hospitalData.getHospitalname());
return hospitalData;
}

public void setHospitalData(HospitalData hospitalData) {
System.out.println(
"In Set method of HospitalEJB" + hospitalData.getHospitalname());
this.hospitalname = hospitalData.getHospitalname();
}

}


Here is my Uniqueid generator class

public class UniqueIdGenerator {



private static long id = System.currentTimeMillis();

public static synchronized long getId() {
return id++;

}

}


Here are my configuration files

Here is the 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>
<session>
<ejb-name>UserManager</ejb-name>
<home>com.cygnet.medina.UserManager.UserManagerHome</home>
<remote>com.cygnet.medina.UserManager.UserManager</remote>
<ejb-class>com.cygnet.medina.UserManager.UserManagerEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>

<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>UserManager</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>


<session>
<ejb-name>HospitalManager</ejb-name>
<home>com.cygnet.medina.HospitalManager.HospitalManagerHome</home>
<remote>com.cygnet.medina.HospitalManager.HospitalManager</remote>
<ejb-class>com.cygnet.medina.HospitalManager.HospitalManagerEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>

<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>HospitalManager</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>

<entity>
<ejb-name>AnesthesiaUser</ejb-name>
<home>com.cygnet.medina.user.AnesthesiaUserHome</home>
<remote>com.cygnet.medina.user.AnesthesiaUser</remote>
<ejb-class>com.cygnet.medina.user.AnesthesiaUserEJB</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Long</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>AnesthesiaUser</abstract-schema-name>
<cmp-field>
<field-name>userid</field-name>
<column-name>userid</column-name>
</cmp-field>
<cmp-field>
<field-name>username</field-name>
<column-name>username</column-name>
</cmp-field>
<cmp-field>
<field-name>password</field-name>
<column-name>password</column-name>
</cmp-field>
<cmp-field>
<field-name>usertype</field-name>
<column-name>usertype</column-name>
</cmp-field>
<cmp-field>
<field-name>createddate</field-name>
<column-name>createddate</column-name>
</cmp-field>
<cmp-field>
<field-name>updateddate</field-name>
<column-name>updateddate</column-name>
</cmp-field>
<primkey-field>userid</primkey-field>
<query>
<query-method>
<method-name>findByUsername</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql></ejb-ql>
</query>

</entity>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>AnesthesiaUser</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
<!-- Added -->

<entity>
<ejb-name>Hospital</ejb-name>
<home>com.cygnet.medina.Hospital.HospitalHome</home>
<remote>com.cygnet.medina.Hospital.Hospital</remote>
<ejb-class>com.cygnet.medina.Hospital.HospitalEJB</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Long</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>Hospital</abstract-schema-name>
<cmp-field>
<field-name>hospitalid</field-name>
<column-name>hospitalid</column-name>
</cmp-field>
<cmp-field>
<field-name>hospitalname</field-name>
<column-name>hospitalname</column-name>
</cmp-field>
<primkey-field>hospitalid</primkey-field>
</entity>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Hospital</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
<!-- End -->

</enterprise-beans>
</ejb-jar>

Here is my jbosscmp-jdbc.xml
<?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">

<!-- ===================================================================== -->
<!-- -->
<!-- Standard JBossCMP-JDBC Configuration -->
<!-- -->
<!-- ===================================================================== -->

<!-- $Id: standardjbosscmp-jdbc.xml,v 1.84.2.6 2005/01/26 18:51:35 starksm Exp $ -->

<jbosscmp-jdbc>

<defaults>
<datasource>java:/MySqlDS</datasource>
<!-- <datasource-mapping>MySQL</datasource-mapping> -->
<create-table>true</create-table>
<remove-table>true</remove-table>
<read-only>false</read-only>
<read-time-out>300000</read-time-out>
<row-locking>false</row-locking>
<pk-constraint>true</pk-constraint>
<fk-constraint>false</fk-constraint>
</defaults>

<enterprise-beans>
<entity>
<ejb-name>AnesthesiaUser</ejb-name>
<pk-constraint>false</pk-constraint>
<table-name>AnesthesiaUser</table-name>
<cmp-field>
<field-name>userid</field-name>
<column-name>userid</column-name>
<jdbc-type>BIGINT</jdbc-type>
<sql-type>BIGINT</sql-type>
</cmp-field>
<cmp-field>
<field-name>username</field-name>
<column-name>username</column-name>
<jdbc-type>VARCHAR</jdbc-type>
<sql-type>VARCHAR(50)</sql-type>
</cmp-field>
<cmp-field>
<field-name>password</field-name>
<column-name>password</column-name>
<jdbc-type>VARCHAR</jdbc-type>
<sql-type>VARCHAR(50)</sql-type>
</cmp-field>
<cmp-field>
<field-name>usertype</field-name>
<column-name>usertype</column-name>
<jdbc-type>VARCHAR</jdbc-type>
<sql-type>VARCHAR(50)</sql-type>
</cmp-field>
<cmp-field>
<field-name>createddate</field-name>
<column-name>createddate</column-name>
<jdbc-type>TIMESTAMP</jdbc-type>
<sql-type>DATETIME</sql-type>
</cmp-field>
<cmp-field>
<field-name>updateddate</field-name>
<column-name>updateddate</column-name>
<jdbc-type>TIMESTAMP</jdbc-type>
<sql-type>DATETIME</sql-type>
</cmp-field>
<query>
<query-method>
<method-name>findByUsername</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<jboss-ql><![CDATA[
SELECT OBJECT(u)
FROM AnesthesiaUser u
WHERE u.username = ?1 ]]></jboss-ql>
</query>
<entity-command name="mysql-get-generated-keys"/>



</entity>

<!-- Added -->
<entity>
<ejb-name>Hospital</ejb-name>
<pk-constraint>false</pk-constraint>
<table-name>Hospital</table-name>
<cmp-field>
<field-name>hospitalid</field-name>
<column-name>hospitalid</column-name>
<jdbc-type>BIGINT</jdbc-type>
<sql-type>BIGINT</sql-type>
</cmp-field>
<cmp-field>
<field-name>hospitalname</field-name>
<column-name>hospitalname</column-name>
<jdbc-type>VARCHAR</jdbc-type>
<sql-type>VARCHAR(50)</sql-type>
</cmp-field>
<entity-command name="mysql-get-generated-keys"/>

</entity>

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

</jbosscmp-jdbc>


I am getting the following error.
The bean is deployed properly.The tables are also created but when I run the application it gives the following error:

18:27:12,093 ERROR [LogInterceptor] TransactionRolledbackException in method: public abstract com.cygnet.medina.Hospital.Hospital com.cygnet.medina.Hospital.HospitalHome.create(com.cygnet.medina.Hospital.HospitalData) throws javax.ejb.CreateException,java.rmi.RemoteException, causedBy:
javax.ejb.EJBException: getGeneratedKeys returned an empty ResultSet
at org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCMySQLCreateCommand.executeInsert(JDBCMySQLCreateCommand.java:87)
at org.jboss.ejb.plugins.cmp.jdbc.JDBCAbstractCreateCommand.performInsert(JDBCAbstractCreateCommand.java:308)
at org.jboss.ejb.plugins.cmp.jdbc.JDBCAbstractCreateCommand.execute(JDBCAbstractCreateCommand.java:138)
at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.createEntity(JDBCStoreManager.java:572)
at org.jboss.ejb.plugins.CMPPersistenceManager.createEntity(CMPPersistenceManager.java:222)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.createEntity(CachedConnectionInterceptor.java:266)
at org.jboss.ejb.EntityContainer.createHome(EntityContainer.java:766)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)

A 0 is entered in the hospitalid field and no value is entered in the hospital name field.


I hope the indentation is properly shown on the forum.
I am struggling with this set up for the past 2 days.

I am using JBoss 4.0.2 and MySQL 4.1.13 as I had told you earlier.

Thanks
With Regards
Adrian
17 years ago
Hi All,
I have a HospitalBean which is called from a HospitalManagerBean which is a SessionBean.

The Manager code is as follows
public HospitalData addHospital(HospitalData hospitalData) throws EJBException,RemoteException{
HospitalData newHospitalData = new HospitalData();

try {
System.out.println("Debug in HospitalManager 1********");
InitialContext initialContext = new InitialContext();
System.out.println("Debug in HospitalManager 2********");
HospitalHome hospitalHome = (HospitalHome) initialContext.lookup(JNDINames.Hospital_Home);
System.out.println("Debug in HospitalManager 3********");
Hospital hospital = hospitalHome.create(hospitalData);
System.out.println("$$$$$$$Created Hospital$$$$$$$$$$$");
System.out.println("Hospital Primary Key" + hospital.getPrimaryKey().toString());
newHospitalData = hospital.getHospitalData();

return newHospitalData;
} catch (NamingException ne) {
throw new EJBException(ne.getMessage());
} catch (CreateException ce) {
throw new EJBException(ce.getMessage());
}
}

The Hospital Entity Bean is as follows
public abstract class HospitalEJB implements EntityBean {

private EntityContext context;
public Long hospitalid;
public String hospitalname;

public void ejbLoad() throws EJBException, RemoteException {

}
public void ejbActivate() throws EJBException, RemoteException {

}

public Long ejbCreate(HospitalData hospitalData) throws CreateException{
Long myid = new Long(UniqueIdGenerator.getId());
System.out.println("My id generated ====== "+myid);
this.hospitalid = myid;
System.out.println("My id generated ====== "+this.hospitalid);
System.out.println("Came in ejbCreate method of HospitalEJB");

this.hospitalname = hospitalData.getHospitalname();
return null;

}

public void ejbPostCreate(HospitalData hospitalData)throws CreateException,RemoteException{
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
System.out.println("Came in EJB POSTCreate of HospitalEJB");
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

}

public void ejbRemove()
throws RemoveException, RemoteException, EJBException {

}

public void setEntityContext(EntityContext context)
throws EJBException, RemoteException {

}

public void unsetEntityContext() throws EJBException, RemoteException {


}

public void ejbPassivate() throws EJBException, RemoteException {

}

public void ejbStore() throws EJBException, RemoteException {

}





/**
* @return
*/
public abstract Long getHospitalid();

/**
* @return
*/
public abstract String getHospitalname();

/**
* @param long1
*/
public abstract void setHospitalid(Long long1);

/**
* @param string
*/
public abstract void setHospitalname(String string);


public HospitalData getHospitalData(){
System.out.println("Came in get hospital data");
System.out.println("In Get method of HospitalEJB"+this.hospitalid);
System.out.println("In Get method of HospitalEJB"+this.hospitalname);

HospitalData hospitalData = new HospitalData();
hospitalData.setHospitalid(this.hospitalid);
hospitalData.setHospitalname(this.hospitalname);
System.out.println("In Get method of HospitalEJB"+hospitalData.getHospitalid());
System.out.println("In Get method of HospitalEJB"+hospitalData.getHospitalname());
return hospitalData;
}


public void setHospitalData(HospitalData hospitalData){
System.out.println("In Set method of HospitalEJB" + hospitalData.getHospitalname());
this.hospitalname = hospitalData.getHospitalname();
}

}


The bean is deployed properly.The tables are also created but when I run the application it gives the following error:

18:27:12,093 ERROR [LogInterceptor] TransactionRolledbackException in method: public abstract com.cygnet.medina.Hospital.Hospital com.cygnet.medina.Hospital.HospitalHome.create(com.cygnet.medina.Hospital.HospitalData) throws javax.ejb.CreateException,java.rmi.RemoteException, causedBy:
javax.ejb.EJBException: getGeneratedKeys returned an empty ResultSet
at org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCMySQLCreateCommand.executeInsert(JDBCMySQLCreateCommand.java:87)
at org.jboss.ejb.plugins.cmp.jdbc.JDBCAbstractCreateCommand.performInsert(JDBCAbstractCreateCommand.java:308)
at org.jboss.ejb.plugins.cmp.jdbc.JDBCAbstractCreateCommand.execute(JDBCAbstractCreateCommand.java:138)
at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.createEntity(JDBCStoreManager.java:572)
at org.jboss.ejb.plugins.CMPPersistenceManager.createEntity(CMPPersistenceManager.java:222)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.createEntity(CachedConnectionInterceptor.java:266)
at org.jboss.ejb.EntityContainer.createHome(EntityContainer.java:766)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)

A 0 is entered in the hospitalid field and no value is entered in the hospital name field.

Here is my 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>
<session>
<ejb-name>UserManager</ejb-name>
<home>com.cygnet.medina.UserManager.UserManagerHome</home>
<remote>com.cygnet.medina.UserManager.UserManager</remote>
<ejb-class>com.cygnet.medina.UserManager.UserManagerEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>

<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>UserManager</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>


<session>
<ejb-name>HospitalManager</ejb-name>
<home>com.cygnet.medina.HospitalManager.HospitalManagerHome</home>
<remote>com.cygnet.medina.HospitalManager.HospitalManager</remote>
<ejb-class>com.cygnet.medina.HospitalManager.HospitalManagerEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>

<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>HospitalManager</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>

<entity>
<ejb-name>AnesthesiaUser</ejb-name>
<home>com.cygnet.medina.user.AnesthesiaUserHome</home>
<remote>com.cygnet.medina.user.AnesthesiaUser</remote>
<ejb-class>com.cygnet.medina.user.AnesthesiaUserEJB</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Long</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>AnesthesiaUser</abstract-schema-name>
<cmp-field>
<field-name>userid</field-name>
<column-name>userid</column-name>
</cmp-field>
<cmp-field>
<field-name>username</field-name>
<column-name>username</column-name>
</cmp-field>
<cmp-field>
<field-name>password</field-name>
<column-name>password</column-name>
</cmp-field>
<cmp-field>
<field-name>usertype</field-name>
<column-name>usertype</column-name>
</cmp-field>
<cmp-field>
<field-name>createddate</field-name>
<column-name>createddate</column-name>
</cmp-field>
<cmp-field>
<field-name>updateddate</field-name>
<column-name>updateddate</column-name>
</cmp-field>
<primkey-field>userid</primkey-field>
<query>
<query-method>
<method-name>findByUsername</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql></ejb-ql>
</query>

</entity>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>AnesthesiaUser</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>


<entity>
<ejb-name>Hospital</ejb-name>
<home>com.cygnet.medina.Hospital.HospitalHome</home>
<remote>com.cygnet.medina.Hospital.Hospital</remote>
<ejb-class>com.cygnet.medina.Hospital.HospitalEJB</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Long</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>Hospital</abstract-schema-name>
<cmp-field>
<field-name>hospitalid</field-name>
<column-name>hospitalid</column-name>
</cmp-field>
<cmp-field>
<field-name>hospitalname</field-name>
<column-name>hospitalname</column-name>
</cmp-field>
<primkey-field>hospitalid</primkey-field>
</entity>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Hospital</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>


</enterprise-beans>
</ejb-jar>

Can anyone tell me what the problem is.I am struggling with it for the past 2 days.
Also FYI I am using Jboss 4.0.2 and MySQL 4.1.13 database
17 years ago
No the id is generated by me in the ejbCreate method using another class

UniqueId generator

Here is the class
package com.cygnet.medina.util;

/**
* @author shardul
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class UniqueIdGenerator {



private static long id = System.currentTimeMillis();

public static synchronized long getId() {
System.out.println(" $$**Came in UniqueId Generator **$$");
System.out.println("Value of id returned is **$$ ====="+ id);
return id++;

}

}

Thanks dave for the reply I hope this is sufficient for you to give me further hint.

Cheers
Adrian
17 years ago
Hi All,
I am using JBoss 4.0.2 and MySQL 4.1.13.
I have deployed my entity bean successfully and the tables are created on the backened.
But when I try to insert data I am getting the following exception.
I have posted my bean and ejb-jar.xml as well as my jbosscmp-jdbc.xml
Can anyone point out what is the mistake?
I feel I am missing some configuration file.



14:33:43,953 ERROR [LogInterceptor] TransactionRolledbackException in method: public abstract com.cygnet.medina.Hospital.Hospital com.cygnet.medina.Hospital.HospitalHome.create(com.cygnet.medina.Hospital.HospitalData) throws javax.ejb.CreateException,java.rmi.RemoteException, causedBy:
javax.ejb.EJBException: getGeneratedKeys returned an empty ResultSet
at org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCMySQLCreateCommand.executeInsert(JDBCMySQLCreateCommand.java:87)
at org.jboss.ejb.plugins.cmp.jdbc.JDBCAbstractCreateCommand.performInsert(JDBCAbstractCreateCommand.java:308)
at org.jboss.ejb.plugins.cmp.jdbc.JDBCAbstractCreateCommand.execute(JDBCAbstractCreateCommand.java:138)
at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.createEntity(JDBCStoreManager.java:572)
at org.jboss.ejb.plugins.CMPPersistenceManager.createEntity(CMPPersistenceManager.java:222)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.createEntity(CachedConnectionInterceptor.java:266)
at org.jboss.ejb.EntityContainer.createHome(EntityContainer.java:766)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.invocation.Invocation.performCall(Invocation.java:345)
at org.jboss.ejb.EntityContainer$ContainerInterceptor.invokeHome(EntityContainer.java:1113)


A null is inserted in the primarykey field.

Here is my Entity Bean code.

public abstract class HospitalEJB implements EntityBean {

private EntityContext context;
public Long hospitalid;
public String hospitalname;

public void ejbLoad() throws EJBException, RemoteException {

}
public void ejbActivate() throws EJBException, RemoteException {

}

public Long ejbCreate(HospitalData hospitalData) throws CreateException{
Long myid = new Long(UniqueIdGenerator.getId());
System.out.println("My id generated ====== "+myid);
this.hospitalid = myid;
System.out.println("My id generated ====== "+this.hospitalid);
System.out.println("Came in ejbCreate method of HospitalEJB");

this.hospitalname = hospitalData.getHospitalname();
return null;

}

public void ejbPostCreate(HospitalData hospitalData)throws CreateException,RemoteException{
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
System.out.println("Came in EJB POSTCreate of HospitalEJB");
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

}

public void ejbRemove()
throws RemoveException, RemoteException, EJBException {

}

public void setEntityContext(EntityContext context)
throws EJBException, RemoteException {

}

public void unsetEntityContext() throws EJBException, RemoteException {


}

public void ejbPassivate() throws EJBException, RemoteException {

}

public void ejbStore() throws EJBException, RemoteException {

}





/**
* @return
*/
public abstract Long getHospitalid();

/**
* @return
*/
public abstract String getHospitalname();

/**
* @param long1
*/
public abstract void setHospitalid(Long long1);

/**
* @param string
*/
public abstract void setHospitalname(String string);


public HospitalData getHospitalData(){
System.out.println("Came in get hospital data");
System.out.println("In Get method of HospitalEJB"+this.hospitalid);
System.out.println("In Get method of HospitalEJB"+this.hospitalname);

HospitalData hospitalData = new HospitalData();
hospitalData.setHospitalid(this.hospitalid);
hospitalData.setHospitalname(this.hospitalname);
System.out.println("In Get method of HospitalEJB"+hospitalData.getHospitalid());
System.out.println("In Get method of HospitalEJB"+hospitalData.getHospitalname());
return hospitalData;
}


public void setHospitalData(HospitalData hospitalData){
System.out.println("In Set method of HospitalEJB" + hospitalData.getHospitalname());
this.hospitalname = hospitalData.getHospitalname();
}

}


Here is my <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>
<session>
<ejb-name>UserManager</ejb-name>
<home>com.cygnet.medina.UserManager.UserManagerHome</home>
<remote>com.cygnet.medina.UserManager.UserManager</remote>
<ejb-class>com.cygnet.medina.UserManager.UserManagerEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>

<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>UserManager</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>


<session>
<ejb-name>HospitalManager</ejb-name>
<home>com.cygnet.medina.HospitalManager.HospitalManagerHome</home>
<remote>com.cygnet.medina.HospitalManager.HospitalManager</remote>
<ejb-class>com.cygnet.medina.HospitalManager.HospitalManagerEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>

<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>HospitalManager</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>

<entity>
<ejb-name>AnesthesiaUser</ejb-name>
<home>com.cygnet.medina.user.AnesthesiaUserHome</home>
<remote>com.cygnet.medina.user.AnesthesiaUser</remote>
<ejb-class>com.cygnet.medina.user.AnesthesiaUserEJB</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Long</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>AnesthesiaUser</abstract-schema-name>
<cmp-field>
<field-name>userid</field-name>
<column-name>userid</column-name>
</cmp-field>
<cmp-field>
<field-name>username</field-name>
<column-name>username</column-name>
</cmp-field>
<cmp-field>
<field-name>password</field-name>
<column-name>password</column-name>
</cmp-field>
<cmp-field>
<field-name>usertype</field-name>
<column-name>usertype</column-name>
</cmp-field>
<cmp-field>
<field-name>createddate</field-name>
<column-name>createddate</column-name>
</cmp-field>
<cmp-field>
<field-name>updateddate</field-name>
<column-name>updateddate</column-name>
</cmp-field>
<primkey-field>userid</primkey-field>
<query>
<query-method>
<method-name>findByUsername</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql></ejb-ql>
</query>

</entity>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>AnesthesiaUser</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
<!-- Added -->

<entity>
<ejb-name>Hospital</ejb-name>
<home>com.cygnet.medina.Hospital.HospitalHome</home>
<remote>com.cygnet.medina.Hospital.Hospital</remote>
<ejb-class>com.cygnet.medina.Hospital.HospitalEJB</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Long</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>Hospital</abstract-schema-name>
<cmp-field>
<field-name>hospitalid</field-name>
<column-name>hospitalid</column-name>
</cmp-field>
<cmp-field>
<field-name>hospitalname</field-name>
<column-name>hospitalname</column-name>
</cmp-field>
<primkey-field>hospitalid</primkey-field>
</entity>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Hospital</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
<!-- End -->

</enterprise-beans>
</ejb-jar>


Here is my jbosscmp-jdbc.xml
<?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">

<!-- ===================================================================== -->
<!-- -->
<!-- Standard JBossCMP-JDBC Configuration -->
<!-- -->
<!-- ===================================================================== -->

<!-- $Id: standardjbosscmp-jdbc.xml,v 1.84.2.6 2005/01/26 18:51:35 starksm Exp $ -->

<jbosscmp-jdbc>

<defaults>
<datasource>java:/MySqlDS</datasource>
<!-- <datasource-mapping>MySQL</datasource-mapping> -->
<create-table>true</create-table>
<remove-table>true</remove-table>
<read-only>false</read-only>
<read-time-out>300000</read-time-out>
<row-locking>false</row-locking>
<pk-constraint>true</pk-constraint>
<fk-constraint>false</fk-constraint>
</defaults>

<enterprise-beans>
<entity>
<ejb-name>AnesthesiaUser</ejb-name>
<pk-constraint>false</pk-constraint>
<table-name>AnesthesiaUser</table-name>
<cmp-field>
<field-name>userid</field-name>
<column-name>userid</column-name>
<jdbc-type>BIGINT</jdbc-type>
<sql-type>BIGINT</sql-type>
</cmp-field>
<cmp-field>
<field-name>username</field-name>
<column-name>username</column-name>
<jdbc-type>VARCHAR</jdbc-type>
<sql-type>VARCHAR(50)</sql-type>
</cmp-field>
<cmp-field>
<field-name>password</field-name>
<column-name>password</column-name>
<jdbc-type>VARCHAR</jdbc-type>
<sql-type>VARCHAR(50)</sql-type>
</cmp-field>
<cmp-field>
<field-name>usertype</field-name>
<column-name>usertype</column-name>
<jdbc-type>VARCHAR</jdbc-type>
<sql-type>VARCHAR(50)</sql-type>
</cmp-field>
<cmp-field>
<field-name>createddate</field-name>
<column-name>createddate</column-name>
<jdbc-type>TIMESTAMP</jdbc-type>
<sql-type>DATETIME</sql-type>
</cmp-field>
<cmp-field>
<field-name>updateddate</field-name>
<column-name>updateddate</column-name>
<jdbc-type>TIMESTAMP</jdbc-type>
<sql-type>DATETIME</sql-type>
</cmp-field>
<query>
<query-method>
<method-name>findByUsername</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<jboss-ql><![CDATA[
SELECT OBJECT(u)
FROM AnesthesiaUser u
WHERE u.username = ?1 ]]></jboss-ql>
</query>
<entity-command name="mysql-get-generated-keys"/>



</entity>

<!-- Added -->
<entity>
<ejb-name>Hospital</ejb-name>
<pk-constraint>false</pk-constraint>
<table-name>Hospital</table-name>
<cmp-field>
<field-name>hospitalid</field-name>
<column-name>hospitalid</column-name>
<jdbc-type>BIGINT</jdbc-type>
<sql-type>BIGINT</sql-type>
</cmp-field>
<cmp-field>
<field-name>hospitalname</field-name>
<column-name>hospitalname</column-name>
<jdbc-type>VARCHAR</jdbc-type>
<sql-type>VARCHAR(50)</sql-type>
</cmp-field>
<entity-command name="mysql-get-generated-keys"/>

</entity>

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

</jbosscmp-jdbc>
Hi All,
I am using JBoss 4.0.2 and MySQL 4.1.13.
I have deployed my entity bean successfully and the tables are created on the backened.
But when I try to insert data I am getting the following exception.


14:33:43,953 ERROR [LogInterceptor] TransactionRolledbackException in method: public abstract com.cygnet.medina.Hospital.Hospital com.cygnet.medina.Hospital.HospitalHome.create(com.cygnet.medina.Hospital.HospitalData) throws javax.ejb.CreateException,java.rmi.RemoteException, causedBy:
javax.ejb.EJBException: getGeneratedKeys returned an empty ResultSet
at org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCMySQLCreateCommand.executeInsert(JDBCMySQLCreateCommand.java:87)
at org.jboss.ejb.plugins.cmp.jdbc.JDBCAbstractCreateCommand.performInsert(JDBCAbstractCreateCommand.java:308)
at org.jboss.ejb.plugins.cmp.jdbc.JDBCAbstractCreateCommand.execute(JDBCAbstractCreateCommand.java:138)
at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.createEntity(JDBCStoreManager.java:572)
at org.jboss.ejb.plugins.CMPPersistenceManager.createEntity(CMPPersistenceManager.java:222)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.createEntity(CachedConnectionInterceptor.java:266)
at org.jboss.ejb.EntityContainer.createHome(EntityContainer.java:766)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.invocation.Invocation.performCall(Invocation.java:345)
at org.jboss.ejb.EntityContainer$ContainerInterceptor.invokeHome(EntityContainer.java:1113)


A null is inserted in the primarykey field.

Here is my Entity Bean code.

public abstract class HospitalEJB implements EntityBean {

private EntityContext context;
public Long hospitalid;
public String hospitalname;

public void ejbLoad() throws EJBException, RemoteException {

}
public void ejbActivate() throws EJBException, RemoteException {

}

public Long ejbCreate(HospitalData hospitalData) throws CreateException{
Long myid = new Long(UniqueIdGenerator.getId());
System.out.println("My id generated ====== "+myid);
this.hospitalid = myid;
System.out.println("My id generated ====== "+this.hospitalid);
System.out.println("Came in ejbCreate method of HospitalEJB");

this.hospitalname = hospitalData.getHospitalname();
return null;

}

public void ejbPostCreate(HospitalData hospitalData)throws CreateException,RemoteException{
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
System.out.println("Came in EJB POSTCreate of HospitalEJB");
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

}

public void ejbRemove()
throws RemoveException, RemoteException, EJBException {

}

public void setEntityContext(EntityContext context)
throws EJBException, RemoteException {

}

public void unsetEntityContext() throws EJBException, RemoteException {


}

public void ejbPassivate() throws EJBException, RemoteException {

}

public void ejbStore() throws EJBException, RemoteException {

}





/**
* @return
*/
public abstract Long getHospitalid();

/**
* @return
*/
public abstract String getHospitalname();

/**
* @param long1
*/
public abstract void setHospitalid(Long long1);

/**
* @param string
*/
public abstract void setHospitalname(String string);


public HospitalData getHospitalData(){
System.out.println("Came in get hospital data");
System.out.println("In Get method of HospitalEJB"+this.hospitalid);
System.out.println("In Get method of HospitalEJB"+this.hospitalname);

HospitalData hospitalData = new HospitalData();
hospitalData.setHospitalid(this.hospitalid);
hospitalData.setHospitalname(this.hospitalname);
System.out.println("In Get method of HospitalEJB"+hospitalData.getHospitalid());
System.out.println("In Get method of HospitalEJB"+hospitalData.getHospitalname());
return hospitalData;
}


public void setHospitalData(HospitalData hospitalData){
System.out.println("In Set method of HospitalEJB" + hospitalData.getHospitalname());
this.hospitalname = hospitalData.getHospitalname();
}

}


Here is my <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>
<session>
<ejb-name>UserManager</ejb-name>
<home>com.cygnet.medina.UserManager.UserManagerHome</home>
<remote>com.cygnet.medina.UserManager.UserManager</remote>
<ejb-class>com.cygnet.medina.UserManager.UserManagerEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>

<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>UserManager</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>


<session>
<ejb-name>HospitalManager</ejb-name>
<home>com.cygnet.medina.HospitalManager.HospitalManagerHome</home>
<remote>com.cygnet.medina.HospitalManager.HospitalManager</remote>
<ejb-class>com.cygnet.medina.HospitalManager.HospitalManagerEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>

<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>HospitalManager</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>

<entity>
<ejb-name>AnesthesiaUser</ejb-name>
<home>com.cygnet.medina.user.AnesthesiaUserHome</home>
<remote>com.cygnet.medina.user.AnesthesiaUser</remote>
<ejb-class>com.cygnet.medina.user.AnesthesiaUserEJB</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Long</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>AnesthesiaUser</abstract-schema-name>
<cmp-field>
<field-name>userid</field-name>
<column-name>userid</column-name>
</cmp-field>
<cmp-field>
<field-name>username</field-name>
<column-name>username</column-name>
</cmp-field>
<cmp-field>
<field-name>password</field-name>
<column-name>password</column-name>
</cmp-field>
<cmp-field>
<field-name>usertype</field-name>
<column-name>usertype</column-name>
</cmp-field>
<cmp-field>
<field-name>createddate</field-name>
<column-name>createddate</column-name>
</cmp-field>
<cmp-field>
<field-name>updateddate</field-name>
<column-name>updateddate</column-name>
</cmp-field>
<primkey-field>userid</primkey-field>
<query>
<query-method>
<method-name>findByUsername</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql></ejb-ql>
</query>

</entity>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>AnesthesiaUser</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
<!-- Added -->

<entity>
<ejb-name>Hospital</ejb-name>
<home>com.cygnet.medina.Hospital.HospitalHome</home>
<remote>com.cygnet.medina.Hospital.Hospital</remote>
<ejb-class>com.cygnet.medina.Hospital.HospitalEJB</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Long</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>Hospital</abstract-schema-name>
<cmp-field>
<field-name>hospitalid</field-name>
<column-name>hospitalid</column-name>
</cmp-field>
<cmp-field>
<field-name>hospitalname</field-name>
<column-name>hospitalname</column-name>
</cmp-field>
<primkey-field>hospitalid</primkey-field>
</entity>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Hospital</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
<!-- End -->

</enterprise-beans>
</ejb-jar>


Here is my jbosscmp-jdbc.xml
<?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">

<!-- ===================================================================== -->
<!-- -->
<!-- Standard JBossCMP-JDBC Configuration -->
<!-- -->
<!-- ===================================================================== -->

<!-- $Id: standardjbosscmp-jdbc.xml,v 1.84.2.6 2005/01/26 18:51:35 starksm Exp $ -->

<jbosscmp-jdbc>

<defaults>
<datasource>java:/MySqlDS</datasource>
<!-- <datasource-mapping>MySQL</datasource-mapping> -->
<create-table>true</create-table>
<remove-table>true</remove-table>
<read-only>false</read-only>
<read-time-out>300000</read-time-out>
<row-locking>false</row-locking>
<pk-constraint>true</pk-constraint>
<fk-constraint>false</fk-constraint>
</defaults>

<enterprise-beans>
<entity>
<ejb-name>AnesthesiaUser</ejb-name>
<pk-constraint>false</pk-constraint>
<table-name>AnesthesiaUser</table-name>
<cmp-field>
<field-name>userid</field-name>
<column-name>userid</column-name>
<jdbc-type>BIGINT</jdbc-type>
<sql-type>BIGINT</sql-type>
</cmp-field>
<cmp-field>
<field-name>username</field-name>
<column-name>username</column-name>
<jdbc-type>VARCHAR</jdbc-type>
<sql-type>VARCHAR(50)</sql-type>
</cmp-field>
<cmp-field>
<field-name>password</field-name>
<column-name>password</column-name>
<jdbc-type>VARCHAR</jdbc-type>
<sql-type>VARCHAR(50)</sql-type>
</cmp-field>
<cmp-field>
<field-name>usertype</field-name>
<column-name>usertype</column-name>
<jdbc-type>VARCHAR</jdbc-type>
<sql-type>VARCHAR(50)</sql-type>
</cmp-field>
<cmp-field>
<field-name>createddate</field-name>
<column-name>createddate</column-name>
<jdbc-type>TIMESTAMP</jdbc-type>
<sql-type>DATETIME</sql-type>
</cmp-field>
<cmp-field>
<field-name>updateddate</field-name>
<column-name>updateddate</column-name>
<jdbc-type>TIMESTAMP</jdbc-type>
<sql-type>DATETIME</sql-type>
</cmp-field>
<query>
<query-method>
<method-name>findByUsername</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<jboss-ql><![CDATA[
SELECT OBJECT(u)
FROM AnesthesiaUser u
WHERE u.username = ?1 ]]></jboss-ql>
</query>
<entity-command name="mysql-get-generated-keys"/>



</entity>

<!-- Added -->
<entity>
<ejb-name>Hospital</ejb-name>
<pk-constraint>false</pk-constraint>
<table-name>Hospital</table-name>
<cmp-field>
<field-name>hospitalid</field-name>
<column-name>hospitalid</column-name>
<jdbc-type>BIGINT</jdbc-type>
<sql-type>BIGINT</sql-type>
</cmp-field>
<cmp-field>
<field-name>hospitalname</field-name>
<column-name>hospitalname</column-name>
<jdbc-type>VARCHAR</jdbc-type>
<sql-type>VARCHAR(50)</sql-type>
</cmp-field>
<entity-command name="mysql-get-generated-keys"/>

</entity>

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

</jbosscmp-jdbc>


Can anyone point out where is the mistake.??
Please I have been stuck on this for 2 days now.
Thanks a lot for all your help.
Regards
Adrian
17 years ago
Hi anyone who has deployed an CMP entity bean on Jboss 4.0.2 with backend as MySQL server.

Can he tell me the steps to deploy the entity bean.
Basically what are the configuration files.

Here are some which I know:

ejb-jar.xml
jbosscmp-jdbc.xml
mysql-ds.xml

Anyone knows whether I am missing out on something???
17 years ago