Hi, Claudio. Thanks for answering.
This is my jboss.xml file:
<?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">
<jboss>
<enterprise-beans>
<!--
To add beans that you have deployment descriptor info for, add
a file to your XDoclet merge directory called jboss-beans.xml that contains
the <session></session>, <entity></entity> and <message-driven></message-driven>
markup for those beans.
-->
<entity>
<ejb-name>User</ejb-name>
<jndi-name>User</jndi-name>
</entity>
<entity>
<ejb-name>Product</ejb-name>
<jndi-name>Product</jndi-name>
<local-jndi-name>ProductLocal</local-jndi-name>
</entity>
<session>
<ejb-name>Adder</ejb-name>
<jndi-name>Adder</jndi-name>
<local-jndi-name>AdderLocal</local-jndi-name>
</session>
</enterprise-beans>
<resource-managers>
</resource-managers>
</jboss>
This is the code used to generate the bean:
import test.interfaces.ProductPK;
import test.interfaces.ProductData;
/**
* The Entity bean represents a TestEntity with BMP
*
* @author Andreas Schaefer
* @version $Revision: 1.1 $
*
* @ejb:bean name="Product"
* display-name="Entity that supports products(BMP)"
* type="BMP"
* jndi-name="Product"
*
* @ejb:env-entry name="DataSourceName"
* value="java:/BelisarioDS"
*
* @ejb:transaction type="Required"
*
* @ejb

ata-object extends="test.interfaces.AbstractData"
* setdata="false"
*
**/
public abstract class ProductBean implements EntityBean {
EntityContext context;
int productId;
String productName;
String description;
double price;
// -------------------------------------------------------------------------
// Methods
// -------------------------------------------------------------------------
/**
* Store the data within the provided data object into this bean.
*
* @param pProductBean The Value Object containing the ProductBean values
*
* @ejb:interface-method view-type="remote"
**/
public void setValueObject( ProductData pProductBean )
throws
InvalidValueException
{
setProductId( pProductBean.getProductId() );
setDescription( pProductBean.getDescription() );
setProductName( pProductBean.getProductName() );
setPrice (pProductBean.getPrice());
}
/**
* Create and return a TestEntity data object populated with the data from
* this bean.
*
* @return Returns a TestEntity value object containing the data within this
* bean.
*
* @ejb:interface-method view-type="remote"
**/
public ProductData getValueObject() {
ProductData lData = new ProductData();
lData.setProductId( getProductId() );
lData.setDescription( getDescription() );
lData.setProductName( getProductName() );
lData.setPrice (getPrice());
return lData;
}
/**
* Describes the instance and its content for debugging purpose
*
* @return Debugging information about the instance and its content
**/
public String toString() {
return "TestBMPEntityBean [ " + getValueObject() + " ]";
}
/**
* Mark the Entity as changed that needs to be saved
**/
protected abstract void makeDirty();
/**
* Mark the Entity as synchronized with the DB and does
* not need to be saved
**/
protected abstract void makeClean();
private void save( boolean pIsNew ) {
Connection con = null;
PreparedStatement ps = null;
String sql = null;
try {
if( pIsNew ) {
// Note that the Primary Key "Id" is the last to match the UPDATE statement
sql = "INSERT INTO Products" +
" (ProductName, Description, Price, ProductId)" +
" VALUES" +
" (?, ?, ?, ?)";
}
else
{
sql ="UPDATE Products" +
" SET ProductName=?, Description=?, Price=?" +
" WHERE ProductId=?"; }
con = getConnection();
ps = con.prepareStatement(sql);
ps.setString(1, getProductName());
ps.setString(2, getDescription());
ps.setDouble(3, getPrice());
ps.setInt(4, getProductId());
ps.executeUpdate();
}
catch(SQLException e) {
//System.out.println(e.toString());
throw new EJBException( "Could not save record to DB: " + e.getMessage() );
}
finally
{
try {
if (ps != null)
ps.close();
if (con != null)
con.close();
}
catch (SQLException e ){
}
}
}
// -------------------------------------------------------------------------
// Properties (Getters/Setters)
// -------------------------------------------------------------------------
/**
* Retrieve the Product�s entity ID.
*
* @return Returns an int representing the id of this TestEntity.
*
* @ejb

ersistent-field
* @ejb

k-field
**/
public abstract int getProductId();
/**
* Set the Product's id.
*
* @param pId The id of this Product. Is set at creation time.
**/
public abstract void setProductId( int pProductId );
/**
* Retrieve the Product�s Name.
*
* @return Returns an int representing the Name of the Product.
*
* @ejb

ersistent-field
**/
public abstract String getProductName();
/**
* Set the entity�s Products�s Name.
*
* @param pFirstName The name of the product. Is set at creation time.
**/
public abstract void setProductName( String pProductName );
/**
* Retrieve the Product�s Description.
*
* @return Returns an int representing the Description of the product.
*
* @ejb

ersistent-field
**/
public abstract String getDescription();
/**
* Set the TestEntity's LastName.
*
* @param pLastName The description of the product. Is set at creation time.
**/
public abstract void setDescription( String pDescription );
/**
* Retrieve the Product�s Price
*
* @return Returns an int representing the Price of the product.
*
* @ejb

ersistent-field
**/
public abstract int getPrice();
/**
* Set the Product�s's LastName.
*
* @param pLastName The priceof the product.
**/
public abstract void setPrice( int pPrice );
// -------------------------------------------------------------------------
// Framework Callbacks
// -------------------------------------------------------------------------
/**
* Create a TestEntity based on the supplied TestEntity Value Object.
*
* @param pTestEntity The data used to create the TestEntity.
*
* @throws InvalidValueException If one of the values are not correct,
* this will not roll back the transaction
* because the caller has the chance to
* fix the problem and try again
* @throws EJBException If no new unique ID could be retrieved this will
* rollback the transaction because there is no
* hope to try again
* @throws CreateException Because we have to do so (
EJB spec.)
*
* @ejb:create-method view-type="remote"
**/
public ProductPK ejbCreate( ProductData pProduct )
throws
InvalidValueException,
EJBException,
CreateException
{
// Clone the given Value Object to keep changed private
ProductData lData = (ProductData) pProduct.clone();
// Save the new TestEntity
setValueObject( lData );
save( true );
// Return the PK which is mandatory in BMPs
return new ProductPK( getProductId() );
}
public void ejbPostCreate(ProductData pProduct )
throws RemoteException, CreateException {
//System.out.println("ejbPostCreate");
}
public ProductPK ejbFindByPrimaryKey(ProductPK primaryKey)
throws RemoteException, FinderException {
//System.out.println("ejbFindByPrimaryKey");
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT ProductName" +
" FROM Products" +
" WHERE ProductId=?";
con = getConnection();
ps = con.prepareStatement(sql);
int lId = primaryKey.productId;
ps.setInt( 1, lId );
rs = ps.executeQuery();
if (rs.next()) {
rs.close();
ps.close();
con.close();
return primaryKey;
}
}
catch (SQLException e) {
System.out.println(e.toString());
}
finally {
try {
if (rs!=null)
rs.close();
if (ps!=null)
ps.close();
if (con!=null)
con.close();
}
catch (SQLException e) {
}
}
throw new ObjectNotFoundException();
}
public Enumeration ejbFindByName(String name)
throws RemoteException, FinderException {
//System.out.println("ejbFindByName");
Vector products = new Vector();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT ProductId " +
" FROM Products" +
" WHERE ProductName=?";
con = getConnection();
ps = con.prepareStatement(sql);
ps.setString(1, name);
rs = ps.executeQuery();
while (rs.next()) {
int productId = rs.getInt(1);
products.addElement(new ProductPK(getProductId()));
}
}
catch (SQLException e) {
System.out.println(e.toString());
}
finally {
try {
if (rs!=null)
rs.close();
if (ps!=null)
ps.close();
if (con!=null)
con.close();
}
catch (SQLException e) {
}
}
return products.elements();
}
public void ejbRemove() throws RemoveException {
//System.out.println("ejbRemove");
Connection con = null;
PreparedStatement ps = null;
try {
String sql = "DELETE FROM Products" +
" WHERE ProductId=?";
con = getConnection();
ps = con.prepareStatement(sql);
int lId = ( (ProductPK) context.getPrimaryKey() ).productId;
ps.setInt( 1, lId );
ps.executeUpdate();
}
catch (SQLException e) {
throw new RemoveException( "Could not remove record from DB: " + e.getMessage() );
}
finally {
try {
if (ps!=null)
ps.close();
if (con!=null)
con.close();
}
catch (SQLException e) {
}
}
}
public void ejbActivate() {
//System.out.println("ejbActivate");
}
public void ejbPassivate() {
//System.out.println("ejbPassivate");
}
public void ejbLoad() {
//System.out.println("ejbLoad");
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT ProductName, Description, Price" +
" FROM Products" +
" WHERE ProductId=?";
con = getConnection();
ps = con.prepareStatement(sql);
int lId = ( (ProductPK) context.getPrimaryKey() ).productId;
ps.setInt( 1, lId );
rs = ps.executeQuery();
if (rs.next()) {
this.productName = rs.getString(1);
this.description = rs.getString(2);
this.price = rs.getDouble(3);
}
makeClean();
}
catch (SQLException e) {
System.out.println(e.toString());
}
finally {
try {
if (rs!=null)
rs.close();
if (ps!=null)
ps.close();
if (con!=null)
con.close();
}
catch (SQLException e) {
}
}
}
public void ejbStore() {
save(false);
}
public void setEntityContext(EntityContext context) {
//System.out.println("setEntityContext");
this.context = context;
}
public void unsetEntityContext() {
//System.out.println("unsetEntityContext");
context = null;
}
public Connection getConnection() {
DataSource myDataSource;
try {
Context lContext = new InitialContext();
String lDataSourceName = (String) lContext.lookup( "java:comp/env/DataSourceName" );
myDataSource = (DataSource) lContext.lookup( lDataSourceName );
return (Connection) myDataSource.getConnection();
}
catch ( NamingException ne ) {
throw new EJBException( "Naming lookup failure: " + ne.getMessage() );
}
catch (SQLException e) {
throw new EJBException( "JDBC Connection failure: " + e.getMessage() );
}
}
}
/*
* Generated by XDoclet - Do not edit!
*/
package test.interfaces;
/**
* Home interface for Product.
*/
public interface ProductHome
extends javax.ejb.EJBHome
{
public static final String COMP_NAME="java:comp/env/ejb/Product";
public static final String JNDI_NAME="Product";
public test.interfaces.Product create(test.interfaces.ProductData pProduct)
throws test.interfaces.InvalidValueException,javax.ejb.CreateException,java.rmi.RemoteException;
public test.interfaces.Product findByPrimaryKey(test.interfaces.ProductPK primaryKey)
throws java.rmi.RemoteException,javax.ejb.FinderException,java.rmi.RemoteException;
public java.util.Enumeration findByName(java.lang.String name)
throws java.rmi.RemoteException,javax.ejb.FinderException,java.rmi.RemoteException;
}
and this the code that XDoclet generates for the Object interface:
*
* Generated by XDoclet - Do not edit!
*/
package test.interfaces;
/**
* Data object for Product.
*/
public class ProductData
extends test.interfaces.AbstractData
implements java.io.Serializable
{
private int productId;
private java.lang.String productName;
private java.lang.String description;
private int price;
public ProductData()
{
}
public ProductData( int productId,java.lang.String productName,java.lang.String description,int price )
{
setProductId(productId);
setProductName(productName);
setDescription(description);
setPrice(price);
}
public ProductData( ProductData otherData )
{
setProductId(otherData.getProductId());
setProductName(otherData.getProductName());
setDescription(otherData.getDescription());
setPrice(otherData.getPrice());
}
public test.interfaces.ProductPK getPrimaryKey() {
test.interfaces.ProductPK pk = new test.interfaces.ProductPK(this.getProductId());
return pk;
}
public int getProductId()
{
return this.productId;
}
public void setProductId( int productId )
{
this.productId = productId;
}
public java.lang.String getProductName()
{
return this.productName;
}
public void setProductName( java.lang.String productName )
{
this.productName = productName;
}
public java.lang.String getDescription()
{
return this.description;
}
public void setDescription( java.lang.String description )
{
this.description = description;
}
public int getPrice()
{
return this.price;
}
public void setPrice( int price )
{
this.price = price;
}
public String toString()
{
StringBuffer str = new StringBuffer("{");
str.append("productId=" + getProductId() + " " + "productName=" + getProductName() + " " + "description=" + getDescription() + " " + "price=" + getPrice());
str.append('}');
return(str.toString());
}
public boolean equals( Object pOther )
{
if( pOther instanceof ProductData )
{
ProductData lTest = (ProductData) pOther;
boolean lEquals = true;
lEquals = lEquals && this.productId == lTest.productId;
if( this.productName == null )
{
lEquals = lEquals && ( lTest.productName == null );
}
else
{
lEquals = lEquals && this.productName.equals( lTest.productName );
}
if( this.description == null )
{
lEquals = lEquals && ( lTest.description == null );
}
else
{
lEquals = lEquals && this.description.equals( lTest.description );
}
lEquals = lEquals && this.price == lTest.price;
return lEquals;
}
else
{
return false;
}
}
public int hashCode()
{
int result = 17;
result = 37*result + (int) productId;
result = 37*result + ((this.productName != null) ? this.productName.hashCode() : 0);
result = 37*result + ((this.description != null) ? this.description.hashCode() : 0);
result = 37*result + (int) price;
return result;
}
}
Thanks!