From: Marco T. <mt...@je...> - 2003-06-22 21:41:48
|
Hi, it's me again. I created the table's relationship as shown by Jboss logging messages (don't consider the Numerator Table which is not related to the problem). 22:25:03,828 INFO [People] Created table 'People' successfully. 22:25:03,828 INFO [Numerator] Table 'Numerator' already exists 22:25:03,906 INFO [Address] Created table 'Address' successfully. 22:25:03,937 INFO [Address] Added foreign key constraint to table 'People' After deployment I find in my database the People and Address tables. People has an index (addressId) which is the pk of Address table. Here follows the code of my entity bean: <!-- BEGIN of CODE --> package org.jemos.core.framework.ejbs.entities; import java.util.Date; import javax.ejb.CreateException; import javax.ejb.EJBException; import javax.ejb.EntityBean; import javax.naming.InitialContext; import javax.naming.NamingException; import org.apache.log4j.Category; import org.jemos.core.framework.ejbs.BaseEntityBean; import org.jemos.core.framework.ejbs.interfaces.AddressData; import org.jemos.core.framework.ejbs.interfaces.AddressLocal; import org.jemos.core.framework.ejbs.interfaces.AddressLocalHome; import org.jemos.core.framework.ejbs.interfaces.PeopleData; import org.jemos.core.framework.ejbs.interfaces.PeopleKey; /** * * @ejb.bean * type="CMP" * name="People" * reentrant="true" * local-jndi-name="ejb/jemos/core/PeopleEJB" * view-type="local" * transaction-type="Container" * cmp-version="2.x" * @ejb.transaction * type="Required" * * @ejb.ejb-ref * ejb-name="Address" * view-type="local" * ref-name="Address" * * * @jboss.persistence * pk-constraint="true" * table-name="people" * * @ejb.value-object * name="PeopleValue" * match="*" * */ public abstract class PeopleBean extends BaseEntityBean implements EntityBean { //The logger instance private transient final Category log = Category.getInstance(getClass().getName()); /** * @ejb.create-method * @param pData * @return * @throws CreateException */ public PeopleKey ejbCreate(PeopleData pData, AddressData aData) throws CreateException { log.info("Now in ejbCreate: Setting People data"); setEmail(pData.getEmail()); setPassword(pData.getPassword()); setFirstName(pData.getFirstName()); setLastName(pData.getLastName()); setCreationDate(new java.util.Date()); return null; } public void ejbPostCreate(PeopleData pData, AddressData aData) throws CreateException { AddressLocal address = createAddress(aData); setHomeAddress(address); } //Business methods /** * @ejb.interface-method */ public AddressLocal createAddress(AddressData aData) throws EJBException { AddressLocal addr = this.getHomeAddress( ); log.info("from setAddress. AddressData:" + aData); try { if (addr == null) { log.info("Creating a new reference to AddressEJB"); // Customer doesn't have an address yet. Create a new one. InitialContext cntx = new InitialContext( ); AddressLocalHome addrHome = (AddressLocalHome)cntx.lookup("ejb/jemos/core/AddressEJB"); log.info("Before creating: addressId value= " + aData.getAddressId()); addr = addrHome.create(aData); log.info("Address created."); } else { // Customer already has an address. Change its fields log.info("Customer already has an address. Change its fields"); addr.setAddress1(aData.getAddress1()); addr.setAddress2(aData.getAddress2()); addr.setAddress3(aData.getAddress3()); addr.setZip(aData.getZip()); addr.setCity(aData.getCity()); addr.setCountry(aData.getCountry()); } } catch (NamingException ne) { throw new EJBException(ne); } catch (CreateException e) { e.printStackTrace(); } finally{ return addr; } } /** * @ejb.interface-method * @return */ public AddressData getAddress() { AddressLocal addrLocal = this.getHomeAddress(); if (addrLocal == null) return null; int addressId = addrLocal.getAddressId(); String address1 = addrLocal.getAddress1(); String address2 = addrLocal.getAddress2(); String address3 = addrLocal.getAddress3(); String zip = addrLocal.getZip(); String city = addrLocal.getCity(); String country = addrLocal.getCountry(); Date date = addrLocal.getCreationDate(); AddressData addrValue = new AddressData(addressId, address1, address2, address3, zip, city, country, date); return addrValue; } //persistent-relationships /** * @ejb.interface-method * view-type="local" * @ejb.relation * name="People-Address" * role-name="1-People-1-Address" * multiple="no" * target-role-name="1-Address-1-People" * target-multiple="no" * target-ejb="Address" * * @jboss.relation * fk-constraint="true" * related-pk-field="addressId" * fk-column="addressId" */ public abstract AddressLocal getHomeAddress(); /** * @ejb.interface-method * @param address */ public abstract void setHomeAddress(AddressLocal address); //abstract-accessor methods /** * @ejb.interface-method * @ejb.persistent-field * @ejb.pk-field * @jboss.persistence * not-null="true" * @jboss.cmp-field * field-name="email" * column-name="email" */ public abstract String getEmail(); /** * @ejb.interface-method * @param email */ public abstract void setEmail(String email); /** * @ejb.interface-method * @ejb.persistent-field * @ejb.pk-field * @jboss.persistence * not-null="true" * @jboss.cmp-field * field-name="password" * column-name="password" */ public abstract String getPassword(); /** * @ejb.interface-method * @param password */ public abstract void setPassword(String password); /** * @ejb.persistent-field * @ejb.interface-method * @jboss.cmp-field * field-name="firstName" * column-name="firstName" */ public abstract String getFirstName(); /** * @ejb.interface-method * @param firstName */ public abstract void setFirstName(String firstName); /** * @ejb.interface-method * @ejb.persistent-field * @jboss.cmp-field * field-name="lastName" * column-name="lastName" */ public abstract String getLastName(); /** * @ejb.interface-method * @param lastName */ public abstract void setLastName(String lastName); //cmr-fields } <!-- END of CODE --> My client creates a reference to the local home interface of the People bean and then creates a People entity: so far so good. As you can see in the ejbPostCreate, then the bean calls the createAddress method which creates an Address entity and returns a reference to the Address local interface. In the ejbPostCreate, then, I call the persitence-relationships accessors (setHomeAddress) but here I obtain the wrong result. While a People entity is written with the data I entered, an Address entity is written with the following values (this has been taken by Jboss console, but the record on the database appears exactly as this): [PeopleDbManagerSession] AddressData: {addressId=0 address1=null address2=null address3=null zip=null city=null country=null creationDate=null These are the values immediately before the home.create(..) method of the Address local bean: <!-- BEGIN of DATA --> 22:27:42,234 INFO [PeopleCMP$Proxy] from setAddress. AddressData:{addressId=1 address1=43, Millais Road address2=Leyton address3=Essex zip=E11 4HB ci ty=London country=UK creationDate=Sun Jun 22 22:27:41 BST 2003} 22:27:42,234 INFO [PeopleCMP$Proxy] Creating a new reference to AddressEJB 22:27:42,234 INFO [PeopleCMP$Proxy] Before creating: addressId value= 1 <!-- END of DATA --> As you can see, the AddressData object contains the values which I entered on my client, but the record on the database is written to null values. I thought that maybe it could be the database (i'm using MySQL 4.0.12-nt) so I changed to SQLServer but the results are the same! Please help me, I think I've looked everywhere, and now I don't know where to look further. Many thanks, Marco |