2 Replies Latest reply on Mar 17, 2004 6:17 AM by captrespect

    CMP composite key help

    captrespect

      Ok, I'm getting desperate here. I have a composite key that I can't get to work. The wierd thing is that half of the key seems to work, but not the other. In other words the SQL generated by JBoss for the getPrimaryKey method is
      SELECT rid from t_movements where rid = ?
      It should be
      SELECT rid, startdate from t_movements where rid = ? and startdate = ?
      It's only using the rid as the key instead of both fields!

      I am using JBoss 3.2 and MS Sqlserver

      I've tried using java.util.Date and java.sql.Date both get the same result.

      Here is a simplified version of my key class

      public class MovementPK implements Serializable {
       public Long rid;
       public java.sql.Date startDate;
       /**
       * Default Constructor
       */
       public MovementPK() {
       }
       /**
       * Constructor
       *
       * @param rid
       * the resident's ID
       * @param startDate
       * the resident's Admission Date
       */
       public MovementPK(Long rid, java.sql.Date startDate) {
       this.rid = rid;
       this.startDate = startDate;
       System.err.println("PKCreated = " + this.toString());
       }
      
       public Long getRid() {
       return this.rid;
       }
       public java.sql.Date getStartDate() {
       return this.startDate;
       }
      public boolean equals(Object obj) {
       if ((obj == null) || !(obj instanceof MovementPK)) {
       return false;
       }
      
       MovementPK other = (MovementPK) obj;
      
       if((other.rid == null)||(other.startDate == null)){
       if (this.rid == null){
       return true;
       } else {
       return false;
       }
       }
       if((this.rid == null)||(other.startDate == null)) {
       if (other.rid == null){
       return true;
       } else {
       return false;
       }
       }
      
       if ((this.rid.equals(other.rid))
       && (this.startDate.equals(other.startDate))) {
       return true;
       } else {
       return false;
       }
       }
      
       public int hashCode() {
       int a;
       int b;
      
      
       if (this.startDate == null) {
       a = 0;
       } else {
       a = startDate.hashCode();
       }
      
       if (this.rid == null){
       b = 0;
       } else {
       b = this.rid.hashCode();
       }
      
      
       return b + a;
      
       }
      
       public String toString() {
       return this.rid + " " + this.startDate;
       }
      }
      


      Here is my ejb-jar.xml
      <entity>
      <ejb-name>MovementEJB</ejb-name>
      <home>movement.MovementHomeRemote</home>
      <remote>movement.MovementRemote</remote>
      <local-home>movement.MovementHomeLocal</local-home>
      <local>movement.beans.movement.MovementLocal</local>
      <ejb-class>movement.MovementBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>movement.MovementPK</prim-key-class>
      <reentrant>false</reentrant>
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>MovementEJB</abstract-schema-name>
      <cmp-field><field-name>rid</field-name></cmp-field>
      <cmp-field><field-name>StartDate</field-name></cmp-field>
      <cmp-field><field-name>EndDate</field-name></cmp-field>
      <cmp-field><field-name>EditDate</field-name></cmp-field>
      <security-identity><use-caller-identity/></security-identity>
      </entity>
      


      and finally the bean class, part of it anyway
      public abstract class MovementBean implements EntityBean {
       public MovementPK ejbCreate(MovementPK pk)
       throws CreateException {
       this.setRid(pk.getRid());
       this.setStartDate(pk.getStartDate());
       return pk;
       }
      
       public void ejbPostCreate(MovementPK pk) {
       }
      
      


      Thanks in advance