1 Reply Latest reply on Jan 19, 2006 8:54 AM by pdog4x4

    Setting relation when Entity Bean has a compound primary key

      I have a Firmware bean with a compound primary key, this bean has a relation to Model bean that has a compound primary key too, like this:

      Firmware
      version (PK)
      model (PK)

      Model
      code (PK)
      manufacturer (PK)

      How can i set a relation between Firmware and Model by only using Firmware.model as foreign key and Mode.code as primary key.

      Actually i'm getting this error:

      org.jboss.deployment.DeploymentException: Mappings were not provided for all fields: unmaped fields=[manufacturer]

      I can't switch to relation-table and i can't use @jboss.auto-key-fields because i have too much code written right now.

      Please somebody help me!

        • 1. Re: Setting relation when Entity Bean has a compound primary
          pdog4x4

          rpa_rio,

          I'm not sure if you have found an answer for your question, but hopefully I can help. This took me a pretty minute to figure out. I use XDOCLET to generate all the helper classes and setup XML. Of course, your mileage may vary.

          The brief setup looks like:


          I did the following for my setup which is similar:

          person.java

          package infrastructure.ejb;
          
          import infrastructure.interfaces.PersonData;
          
          import java.rmi.RemoteException;
          
          import javax.ejb.EJBException;
          import javax.ejb.EntityBean;
          import javax.ejb.EntityContext;
          import javax.ejb.RemoveException;
          
          /**
           * @ejb.bean name="Person"
           * display-name="PersonBean"
           * description="Any person who is authorized to gain access."
           * jndi-name="infrastructure/ejb/Person"
           * local-jndi-name = "infrastructure/ejb/PersonLocal"
           * type="CMP"
           * cmp-version="2.x"
           * view-type="both"
           *
           * @ejb.persistence table-name = "Person"
           *
           * @ejb.finder signature = "java.util.Collection findAll()"
           *
           * @ejb.finder signature = "infrastructure.interfaces.PersonLocal findByEmail(java.lang.String email)"
           * query = "SELECT OBJECT(o) FROM Person AS o WHERE o.email= ?1"
           *
           * @ejb.finder signature = "infrastructure.interfaces.PersonLocal findByUsername(java.lang.String username)"
           * query = "SELECT DISTINCT OBJECT(o) FROM Person AS o WHERE o.username= ?1"
           *
           * @ejb.finder signature = "java.util.Collection findByLastname(java.lang.String lastname)"
           * query = "SELECT OBJECT(o) FROM Person AS o WHERE o.lastname= ?1"
           *
           * @ejb.pk class = "infrastructure.interfaces.PersonPK"
           * package = "infrastructure.interfaces"
           *
           * @jboss.declared-sql signature = "java.util.Collection findAll()"
           *
           * @jboss.declared-sql signature = "infrastructure.interfaces.PersonLocal findByEmail(java.lang.String email)"
           * where = "pk_person={0}"
           *
           * @jboss.declared-sql signature = "infrastructure.interfaces.PersonLocal findByUsername(java.lang.String username)"
           * where = "username={0}"
           *
           * @jboss.declared-sql signature = "java.util.Collection findByLastname(java.lang.String lastname)"
           * where = "lastname={0}"
           *
           * @jboss.persistence table-name = "Person"
           * alter-table = "true"
           * pk-constraint = "true"
           * create-table = "true"
           * remove-table = "true"
           *
           * @jboss.tuned-updates "true"
           */
          public abstract class PersonBean implements EntityBean {
          
           public PersonBean() {
           super();
           }
          
           public void setEntityContext(EntityContext ctx)
           throws EJBException,
           RemoteException {
           }
          
           public void unsetEntityContext() throws EJBException, RemoteException {
           }
          
           public void ejbRemove()
           throws RemoveException,
           EJBException,
           RemoteException {
           }
          
           public void ejbActivate() throws EJBException, RemoteException {
           }
          
           public void ejbPassivate() throws EJBException, RemoteException {
           }
          
           public void ejbLoad() throws EJBException, RemoteException {
           }
          
           public void ejbStore() throws EJBException, RemoteException {
           }
          
           /**
           * Getter for CMP Field firstname
           *
           *
           * @ejb.persistent-field
           * @ejb.interface-method view-type="both"
           */
           public abstract String getFirstname();
          
           /**
           * Setter for CMP Field firstname
           *
           * @ejb.interface-method view-type="both"
           */
           public abstract void setFirstname(String value);
          
           /**
           * Getter for CMP Field lastname
           *
           *
           * @ejb.persistent-field
           * @ejb.interface-method view-type="both"
           */
           public abstract String getLastname();
          
           /**
           * Setter for CMP Field lastname
           *
           * @ejb.interface-method view-type="both"
           */
           public abstract void setLastname(String value);
          
           /**
           * Getter for CMP Field email
           *
           * @ejb.persistent-field
           *
           * @ejb.pk-field
           * @ejb.interface-method view-type="both"
           * @jboss.persistence dbindex = "true"
           */
           public abstract String getEmail();
          
           /**
           * Setter for CMP Field email
           *
           * @ejb.interface-method view-type="both"
           */
           public abstract void setEmail(String value);
          
           /**
           * Getter for CMP Field username
           *
           *
           * @ejb.persistent-field
           * @ejb.pk-field
           * @ejb.interface-method view-type="both"
           * @jboss.persistence dbindex = "true"
           */
           public abstract String getUsername();
          
           /**
           * Setter for CMP Field username
           *
           * @ejb.interface-method view-type="both"
           */
           public abstract void setUsername(String value);
          
           /**
           * Getter for CMP Field password
           *
           *
           * @ejb.persistent-field
           * @ejb.interface-method view-type="both"
           */
           public abstract String getPassword();
          
           /**
           * Setter for CMP Field password
           *
           * @ejb.interface-method view-type="both"
           */
           public abstract void setPassword(String value);
          
           /**
           * Create method
           * @ejb.create-method view-type = "local"
           */
           public infrastructure.interfaces.PersonPK ejbCreate(PersonData data)
           throws javax.ejb.CreateException {
          
           this.setEmail(data.getEmail());
           this.setFirstname(data.getFirstname());
           this.setLastname(data.getLastname());
           this.setPassword(data.getPassword());
           this.setUsername(data.getUsername());
          
           return null;
           }
           /**
           * Post Create method
           */
           public void ejbPostCreate(PersonData data) throws javax.ejb.CreateException {
           // TODO Auto-generated method stub
           }
          
          }
          


          product.java
          package infrastructure.ejb;
          
          import infrastructure.interfaces.PersonLocal;
          import infrastructure.interfaces.ProductData;
          
          import java.rmi.RemoteException;
          
          import javax.ejb.EJBException;
          import javax.ejb.EntityBean;
          import javax.ejb.EntityContext;
          import javax.ejb.RemoveException;
          
          /**
           * @ejb.bean name="Product"
           * display-name="A Product"
           * description="A product, is not a product persay."
           * jndi-name="infrastructure/ejb/Product"
           * local-jndi-name = "infrastructure/ejb/ProductLocal"
           * type="CMP"
           * cmp-version="2.x"
           * view-type="both"
           *
           * @ejb.finder query="SELECT OBJECT(o) FROM Product AS o"
           * signature="java.util.Collection findAll()"
           *
           * @ejb.finder query="SELECT OBJECT(o) FROM Product AS o WHERE o.code= ?1"
           * signature="java.util.Collection findByCode(java.lang.String code)"
           *
           * @ejb.persistence table-name = "Product"
           *
           * @ejb.pk class = "infrastructure.interfaces.ProductPK"
           * package = "infrastructure.interfaces"
           *
           * @jboss.persistence alter-table = "true"
           * create-table = "true"
           * pk-constraint = "true"
           * remove-table = "false"
           * table-name = "Product"
           *
           * @jboss.tuned-updates "true"
           */
          public abstract class ProductBean implements EntityBean {
          
           public ProductBean() {
           super();
           // TODO Auto-generated constructor stub
           }
          
           public void setEntityContext(EntityContext ctx)
           throws EJBException,
           RemoteException {
           // TODO Auto-generated method stub
          
           }
          
           public void unsetEntityContext() throws EJBException, RemoteException {
           // TODO Auto-generated method stub
          
           }
          
           public void ejbRemove()
           throws RemoveException,
           EJBException,
           RemoteException {
           // TODO Auto-generated method stub
          
           }
          
           public void ejbActivate() throws EJBException, RemoteException {
           // TODO Auto-generated method stub
          
           }
          
           public void ejbPassivate() throws EJBException, RemoteException {
           // TODO Auto-generated method stub
          
           }
          
           public void ejbLoad() throws EJBException, RemoteException {
           // TODO Auto-generated method stub
          
           }
          
           public void ejbStore() throws EJBException, RemoteException {
           // TODO Auto-generated method stub
          
           }
          
           /**
           * Create method
           * @ejb.create-method view-type = "both"
           */
           public infrastructure.interfaces.ProductPK ejbCreate(ProductData data)
           throws javax.ejb.CreateException {
           this.setCode(data.getCode());
           this.setFullname(data.getFullname());
          
           return null;
           }
           /**
           * Post Create method
           */
           public void ejbPostCreate(ProductData data)
           throws javax.ejb.CreateException {
           // TODO Auto-generated method stub
           }
           /**
           * Getter for CMP Field code
           *
           *
           * @ejb.persistent-field
           * @ejb.pk-field
           * @ejb.interface-method view-type="both"
           * @jboss.persistence dbindex = "true"
           */
           public abstract java.lang.String getCode();
          
           /**
           * Setter for CMP Field code
           *
           * @ejb.interface-method view-type="both"
           */
           public abstract void setCode(java.lang.String value);
          
           /**
           * Getter for CMP Field fullname
           *
           *
           * @ejb.persistent-field
           * @ejb.pk-field
           * @ejb.interface-method view-type="both"
           * @jboss.persistence dbindex = "true"
           */
           public abstract java.lang.String getFullname();
          
           /**
           * Setter for CMP Field fullname
           *
           * @ejb.interface-method view-type="both"
           */
           public abstract void setFullname(java.lang.String value);
          
           /**
           * Getter for CMP Field modified_date
           *
           *
           * @ejb.persistent-field
           * @ejb.interface-method view-type="both"
           */
           public abstract java.util.Date getModified_date();
          
           /**
           * Setter for CMP Field modified_date
           *
           * @ejb.interface-method view-type="both"
           */
           public abstract void setModified_date(java.util.Date value);
          
           /**
           * Getter for CMR Relationship
           *
           * @ejb.interface-method view-type="both"
           * @ejb.relation name = "ProductsForAPerson"
           * role-name = "ProductForAPerson"
           * target-role-name = "PersonHasProducts"
           * target-cascade-delete = "no"
           * target-ejb = "Person"
           *
           * @jboss.relation fk-column = "fk_person_email"
           * fk-constraint = "true"
           * related-pk-field = "email"
           * @jboss.relation fk-column = "fk_person_username"
           * fk-constraint = "true"
           * related-pk-field = "username"
           */
           public abstract PersonLocal getModifiedPerson();
          
           /**
           * Setter for CMR Relationship
           *
           * @ejb.interface-method view-type="both"
           */
           public abstract void setModifiedPerson(PersonLocal value);
          }
          


          The key is that for compound keys, you must have multiple foreign key mappings, which is accomplished by using multiple @jboss.relation tags.

          Hope that helps!

          Joshua Preston.