0 Replies Latest reply on Aug 1, 2003 12:32 AM by pergesu

    Simple CMR one-to-one unidirectional relationship

    pergesu Newbie

      At work I was handed a copy of Enterprise JavaBeans 3rd Ed. and told to convert an app written entirely of regular JavaBeans and JDBC calls into EJB. I haven't had too much trouble, but I've had some problems with relationships. This one should be really simple, but I haven't been able to set it up correctly. The two beans in this relationship represent an Account and a CreditCard. Each bean has a primary key named accid, and they should be equivalent. The accid in Account is generated by the database, by auto_increment. The CreditCard accid should come from the Account. As I understand it, when the AccountLocal is created, a corresponding CreditCardLocal should be as well. I get various problems when I try different stuff, so I'll just post the relevant code, and hopefully somebody can tell me what's goin on.

      ************** ejb-jar.xml ****************

      <ejb-jar>
      <enterprise-beans>

      <ejb-name>CreditCardEJB</ejb-name>
      <local-home>com.blazingmedia.xplode.account.ejb.CreditCardHomeLocal</local-home>
      com.blazingmedia.xplode.account.ejb.CreditCardLocal
      <ejb-class>com.blazingmedia.xplode.account.ejb.CreditCardBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.Integer</prim-key-class>
      False
      <abstract-schema-name>CreditCard</abstract-schema-name>
      <cmp-field><field-name>accid</field-name></cmp-field>
      <cmp-field><field-name>ccCVVS</field-name></cmp-field>
      <cmp-field><field-name>ccExpire</field-name></cmp-field>
      <cmp-field><field-name>ccName</field-name></cmp-field>
      <cmp-field><field-name>ccNumber</field-name></cmp-field>
      <cmp-field><field-name>ccType</field-name></cmp-field>
      <primkey-field>accid</primkey-field>
      <security-identity><use-caller-identity/></security-identity>



      <ejb-name>AccountEJB</ejb-name>
      <local-home>com.blazingmedia.xplode.account.ejb.AccountHomeLocal</local-home>
      com.blazingmedia.xplode.account.ejb.AccountLocal
      <ejb-class>com.blazingmedia.xplode.account.ejb.AccountBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.Integer</prim-key-class>
      False
      <abstract-schema-name>Account</abstract-schema-name>
      <cmp-field><field-name>accid</field-name></cmp-field>
      <cmp-field><field-name>username</field-name></cmp-field>
      <cmp-field><field-name>firstName</field-name></cmp-field>
      <cmp-field><field-name>lastName</field-name></cmp-field>
      <cmp-field><field-name>street1</field-name></cmp-field>
      <cmp-field><field-name>street2</field-name></cmp-field>
      <cmp-field><field-name>city</field-name></cmp-field>
      <cmp-field><field-name>state</field-name></cmp-field>
      <cmp-field><field-name>zip</field-name></cmp-field>
      <cmp-field><field-name>country</field-name></cmp-field>
      <cmp-field><field-name>phone</field-name></cmp-field>
      <cmp-field><field-name>billType</field-name></cmp-field>
      <cmp-field><field-name>planType</field-name></cmp-field>
      <cmp-field><field-name>date</field-name></cmp-field>
      <cmp-field><field-name>fax</field-name></cmp-field>
      <cmp-field><field-name>company</field-name></cmp-field>
      <cmp-field><field-name>referredBy</field-name></cmp-field>
      <cmp-field><field-name>active</field-name></cmp-field>
      <cmr-field><cmr-field-name>creditCard</cmr-field-name></cmr-field>
      <primkey-field>accid</primkey-field>
      <security-identity><use-caller-identity/></security-identity>



      <ejb-relation>
      <ejb-relation-name>Account-CreditCard</ejb-relation-name>
      <ejb-relationship-role>
      <ejb-relationship-role-name>
      Account-has-a-CreditCard
      </ejb-relationship-role-name>
      One
      <relationship-role-source>
      <ejb-name>AccountEJB</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>creditCard</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>
      CreditCard-belongs-to-Account
      </ejb-relationship-role-name>
      One
      <cascade-delete/>
      <relationship-role-source>
      <ejb-name>CreditCardEJB</ejb-name>
      </relationship-role-source>
      </ejb-relationship-role>
      </ejb-relation>

      </ejb-jar>

      *************** ejb-jar.xml ****************

      *************** jbosscmp-jdbc.xml ****************

      <jbosscmp-jdbc>
      <enterprise-beans>

      <ejb-name>CreditCardEJB</ejb-name>
      <table-name>CreditCards</table-name>
      <cmp-field><field-name>accid</field-name></cmp-field>
      <cmp-field><field-name>ccCVVS</field-name></cmp-field>
      <cmp-field><field-name>ccExpire</field-name></cmp-field>
      <cmp-field><field-name>ccName</field-name></cmp-field>
      <cmp-field><field-name>ccNumber</field-name></cmp-field>
      <cmp-field><field-name>ccType</field-name></cmp-field>
      <entity-command name="mysql-get-generated-keys"/>



      <ejb-name>AccountEJB</ejb-name>
      <table-name>Accounts</table-name>
      <cmp-field><field-name>accid</field-name><auto-increment/></cmp-field>
      <cmp-field><field-name>username</field-name></cmp-field>
      <cmp-field><field-name>firstName</field-name></cmp-field>
      <cmp-field><field-name>lastName</field-name></cmp-field>
      <cmp-field><field-name>street1</field-name></cmp-field>
      <cmp-field><field-name>street2</field-name></cmp-field>
      <cmp-field><field-name>city</field-name></cmp-field>
      <cmp-field><field-name>state</field-name></cmp-field>
      <cmp-field><field-name>zip</field-name></cmp-field>
      <cmp-field><field-name>country</field-name></cmp-field>
      <cmp-field><field-name>phone</field-name></cmp-field>
      <cmp-field><field-name>billType</field-name></cmp-field>
      <cmp-field><field-name>planType</field-name></cmp-field>
      <cmp-field><field-name>date</field-name></cmp-field>
      <cmp-field><field-name>fax</field-name></cmp-field>
      <cmp-field><field-name>company</field-name></cmp-field>
      <cmp-field><field-name>referredBy</field-name></cmp-field>
      <cmp-field><field-name>active</field-name></cmp-field>
      <entity-command name="mysql-get-generated-keys"/>


      </enterprise-beans>


      <ejb-relation>
      <ejb-relation-name>Account-CreditCard</ejb-relation-name>
      <foreign-key-mapping/>
      <ejb-relationship-role>
      <ejb-relationship-role-name>Account-has-a-CreditCard</ejb-relationship-role-name>
      <key-fields>
      <key-field>
      <field-name>accid</field-name>
      <column-name>accid</column-name>
      </key-field>
      </key-fields>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>CreditCard-belongs-to-Account</ejb-relationship-role-name>
      <key-fields/>
      </ejb-relationship-role>
      </ejb-relation>

      </jbosscmp-jdbc>

      **************** jbosscmp-jdbc.xml ***************

      **************** CreditCardBean.java ***************

      public abstract class CreditCardBean implements EntityBean
      {
      public Integer ejbCreate() throws CreateException
      {
      return null;
      }

      public void ejbPostCreate()
      {
      }

      public abstract Integer getAccid();

      public abstract String getCcCVVS();

      public abstract String getCcExpire();

      public abstract String getCcName();

      public abstract String getCcNumber();

      public abstract String getCcType();

      public abstract void setAccid(Integer i);

      public abstract void setCcCVVS(String string);

      public abstract void setCcExpire(String string);

      public abstract void setCcName(String string);

      public abstract void setCcNumber(String string);

      public abstract void setCcType(String string);

      public void setEntityContext(EntityContext context) { }
      public void unsetEntityContext() { }
      public void ejbLoad() { }
      public void ejbStore() { }
      public void ejbActivate() { }
      public void ejbPassivate() { }
      public void ejbRemove() { }
      }

      ****************** CreditCardBean.java ****************

      **************** CreditCardLocal.java ****************

      public interface CreditCardLocal extends EJBLocalObject
      {
      public Integer getAccid() throws EJBException;
      public String getCcCVVS() throws EJBException;
      public void setCcCVVS(String ccCVVS) throws EJBException;
      public String getCcExpire() throws EJBException;
      public void setCcExpire(String ccExpire) throws EJBException;
      public String getCcName() throws EJBException;
      public void setCcName(String ccName) throws EJBException;
      public String getCcNumber() throws EJBException;
      public void setCcNumber(String ccNumber) throws EJBException;
      public String getCcType() throws EJBException;
      public void setCcType(String ccType) throws EJBException;
      }

      ****************** CreditCardLocal.java ****************

      *************** CreditCardHomeLocal.java ****************

      public interface CreditCardHomeLocal extends EJBLocalHome
      {
      public CreditCardLocal create()
      throws CreateException, EJBException;

      public CreditCardLocal findByPrimaryKey(Integer pk)
      throws FinderException, EJBException;
      }

      ****************** CreditCardHomeLocal.java **************