Hi everyone:
I write a cmp EJB.It is a one-one map.The customerEJB has a address field and column.I want to create a addressEJB at the same time when I create a CustomerEjB.So I add a setAddress method in CustomerEJB.The code is:
/////////////////////////CustomerBean.java/////////////////////////
public abstract AddressRemote getAddressho()throws RemoteException;
public abstract void setAddressho(AddressRemote address)throws RemoteException;
public abstract void setId(Integer id);
public abstract Integer getId();
public void ejbLoad(){}
public void ejbActivate(){}
public void ejbPassivate(){}
public void ejbRemove(){}
public void ejbStore(){}
public void setEntityContext(EntityContext ctx){
this.ctx=ctx;
}
public void unsetEntityContext(){
this.ctx=null;
}
public void setAddress(
String homeaddress,String companyaddress)throws RemoteException{
System.out.println("Enter setAddress...");
try{
AddressRemote addr=this.getAddressho();
if(null==addr){
System.out.println("Enter addr==null");
Context ctx=new InitialContext();
System.out.println("Before lookup ejb/addressentity");
AddressHome addrh=(AddressHome)PortableRemoteObject.narrow(ctx.lookup("ejb/AddressEntity"),AddressHome.class);
System.out.println("After lookup ejb/addressentity");
addr=addrh.create(homeaddress,companyaddress);
this.setAddressho(addr);
System.out.println("After setaddress....");
}else{
addr.setHomeaddress(homeaddress);
addr.setCompanyaddress(companyaddress);
}
........................................................................
/////////////////////////End////////////////////////////////////////
The addressho is a cmr field that customer use it find addressEJB.
My jbosscmp-jdbc.xml is:
////////////////////////////////////////////////////////////////////
<enterprise-beans>
<entity>
<ejb-name>Addressentity</ejb-name>
<table-name>address</table-name>
<cmp-field>
<field-name>id</field-name>
<column-name>id</column-name>
<not-null/>
</cmp-field>
<cmp-field>
<field-name>homeaddress</field-name>
<column-name>homeaddress</column-name>
</cmp-field>
<cmp-field>
<field-name>companyaddress</field-name>
<column-name>companyaddress</column-name>
</cmp-field>
</entity>
<entity>
<ejb-name>Customerentity</ejb-name>
<table-name>customer</table-name>
<cmp-field>
<field-name>id</field-name>
<column-name>id</column-name>
<not-null/>
</cmp-field>
<cmp-field>
<field-name>name</field-name>
<column-name>name</column-name>
</cmp-field>
<cmp-field>
<field-name>age</field-name>
<column-name>age</column-name>
</cmp-field>
<cmp-field>
<field-name>email</field-name>
<column-name>email</column-name>
</cmp-field>
</entity>
</enterprise-beans>
<relationships>
<ejb-relation>
<ejb-relation-name>Customer-Address</ejb-relation-name>
<foreign-key-mapping/>
<ejb-relationship-role>
<ejb-relationship-role-name>customer-has-one-address</ejb-relationship-role-name>
<key-fields/>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>address-belonge-one-customer</ejb-relationship-role-name>
<fk-constraint>true</fk-constraint>
<key-fields>
<key-field>
<field-name>id</field-name>
<column-name>address_id</column-name>
</key-field>
</key-fields>
</ejb-relationship-role>
</ejb-relation>
</relationships>
I use MySQL database.The table customer is:
id int not null primary key auto_increment,name varchar(100),age int,email varchar(100),address_id int);
The address table is:
id int not null primary key auto_increment,homeaddress varchar(100),companyaddress varchar(100));
The ejb-jar.xml is:
////////////////////////////////////////////////////////////////////
<entity>
<description>addresscmp bean</description>
<display-name>addressbean</display-name>
<ejb-name>Addressentity</ejb-name>
<home>lyo.hotmail.cmp.AddressHome</home>
<remote>lyo.hotmail.cmp.AddressRemote</remote>
<ejb-class>lyo.hotmail.cmp.AddressBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>Addressschema</abstract-schema-name>
<cmp-field>
<description>address id primary key</description>
<field-name>id</field-name>
</cmp-field>
<cmp-field>
<description>addresshome</description>
<field-name>homeaddress</field-name>
</cmp-field>
<cmp-field>
<field-name>companyaddress</field-name>
</cmp-field>
<primkey-field>id</primkey-field>
</entity>
<entity>
<description>customer bean</description>
<display-name>customerbean</display-name>
<ejb-name>Customerentity</ejb-name>
<home>lyo.hotmail.cmp.CustomerHome</home>
<remote>lyo.hotmail.cmp.CustomerRemote</remote>
<ejb-class>lyo.hotmail.cmp.CustomerBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>Customerschema</abstract-schema-name>
<cmp-field>
<description>customer id primary key</description>
<field-name>id</field-name>
</cmp-field>
<cmp-field>
<description>name</description>
<field-name>name</field-name>
</cmp-field>
<cmp-field>
<field-name>age</field-name>
</cmp-field>
<cmp-field>
<field-name>email</field-name>
</cmp-field>
<primkey-field>id</primkey-field>
</entity>
</enterprise-beans>
<relationships>
<ejb-relation>
<ejb-relation-name>Customer-Address</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>
customer-has-one-address
</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<relationship-role-source>
<ejb-name>Customerentity</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>addressho</cmr-field-name>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>
address-belonge-one-customer
</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<relationship-role-source>
<ejb-name>Addressentity</ejb-name>
</relationship-role-source>
</ejb-relationship-role>
</ejb-relation>
</relationships>
When I use
Tomcat to
test it.The
Jboss console report error:
/////////////////////////////////////////////////////////////////////
debug before addresshome
debug..beforesetaddress
Exception:java.rmi.ServerException: RemoteException occurred in server
thread; nested exception is: java.rmi.ServerException: RuntimeException; nested exception is: java.lang.IllegalArgumentException: Attempt to get a lock for a null object
/////////////////////////////////////////////////////////////
But the data has been insert into database successfual!Why?
The
jsp code is:
/////////////////////testejb.jsp//////////////////////////////////
Integer i=new Integer(21);
Integer in=new Integer(3);
CustomerHome home=(CustomerHome)PortableRemoteObject.narrow(ctx.lookup("ejb/CustomerEntity"),CustomerHome.class);
CustomerRemote re=home.create("yong",i,"li@yahoo.com");
out.println("debug before addresshome");
out.println("debug..beforesetaddress");
re.setAddress("beijinghaidian","zhanlan@@@guan");
out.println("debug after setAddress()");
Integer io=new Integer(1);
out.println("debug...before findBypri by customer");
CustomerRemote ree=home.findByPrimaryKey(in);
out.println("Get:"+ree.getName());
out.println("and:"+ree.getAge());
out.println("Email:"+ree.getEmail());
Why ? How to fix it?Thks :roll: