4 Replies Latest reply on Nov 8, 2006 3:27 PM by monkeyden

    Wrong query generated

    monkeyden

      For some reason, the resulting query from this entity has the ACTUAL DB field names for a couple of the non-pk fields (see SEQ_NUMBER, LAST_UPDATE). Can anyone give a hint as to what might be the problem? Thanks.
      Query

      select streetwatc0_.userKey as userKey1292_, streetwatc0_.locationId as locationId1292_, streetwatc0_.areaId as areaId1292_, streetwatc0_.addressStandardized as addressS4_1292_, streetwatc0_.SEQ_NUMBER as SEQ5_1292_, streetwatc0_.LAST_UPDATE as LAST6_1292_, streetwatc0_.ADDRESS as ADDRESS1292_
      from DWL_STREET_WATCH_LIST streetwatc0_
      where streetwatc0_.userKey=?


      Client code
      Query query = em.createQuery("from StreetWatchEntity where userKey = :userKey");
       query.setParameter("userKey", userKey);
       entities = query.getResultList();


      Entity
      package com.company.project.entity;
      
      import java.util.Date;
      
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.Id;
      import javax.persistence.IdClass;
      import javax.persistence.NamedQuery;
      import javax.persistence.Table;
      
      import org.jboss.seam.annotations.Name;
      
      @NamedQuery(
       name="orderedStreets",
       query="from StreetWatchEntity order by seqNumber"
      )
      
      @Entity
      @Name("streetWatchEntity")
      @Table(name = "STREET_WATCH_LIST")
      @IdClass(StreetWatchPK.class)
      public class StreetWatchEntity implements java.io.Serializable {
       private static final long serialVersionUID = 4855048029589330487L;
      
       public StreetWatchEntity(){}
      
       public StreetWatchEntity(Long userKey, Long locationId, String address, String addressStandardized, Date lastUpdate, Long seqNumber, Long areaId) {
       this.userKey = userKey;
       this.locationId = locationId;
       this.address = address;
       this.addressStandardized = addressStandardized;
       this.lastUpdate = lastUpdate;
       this.seqNumber = seqNumber;
       this.areaId = areaId;
       }
      
       /**
       * Retrieves the UserKey from the entity
       */
       @Id
       @Column(name = "USER_KEY", length = 22, nullable = false)
       public Long getUserKey() {
       return this.userKey;
       }
      
       /**
       * Sets the UserKey of the entity
       */
       public void setUserKey(Long userKey) {
       this.userKey = userKey;
       }
      
       /**
       * Retrieves the LocationId from the entity
       */
       @Id
       @Column(name = "LOCATION_ID", length = 19, nullable = false)
       public Long getLocationId() {
       return this.locationId;
       }
      
       /**
       * Sets the LocationId of the entity
       */
       public void setLocationId(Long locationId) {
       this.locationId = locationId;
       }
      
       /**
       * Retrieves the AddressStandardized from the entity
       */
       @Id
       @Column(name = "ADDRESS_STANDARDIZED", length = 128, nullable = false)
       public String getAddressStandardized() {
       return this.addressStandardized;
       }
      
       /**
       * Sets the AddressStandardized of the entity
       */
       public void setAddressStandardized(String addressStandardized) {
       this.addressStandardized = addressStandardized;
       }
      
       /**
       * Retrieves the AreaId from the entity
       */
       @Id
       @Column(name = "AREA_ID", length = 22, nullable = false)
       public Long getAreaId() {
       return this.areaId;
       }
      
       /**
       * Sets the AreaId of the entity
       */
       public void setAreaId(Long areaId) {
       this.areaId = areaId;
       }
      
       /**
       * Retrieves the Address from the entity
       */
       @Column(name = "ADDRESS", length = 128, nullable = true)
       public String getAddress() {
       return this.address;
       }
      
       /**
       * Sets the Address of the entity
       */
       public void setAddress(String address) {
       this.address = address;
       }
      
       /**
       * Retrieves the LastUpdate from the entity
       */
       @Column(name = "LAST_UPDATE", length = 7, nullable = true)
       public Date getLastUpdate() {
       return this.lastUpdate;
       }
      
       /**
       * Sets the LastUpdate of the entity
       */
       public void setLastUpdate(Date lastUpdate) {
       this.lastUpdate = lastUpdate;
       }
      
       /**
       * Retrieves the SeqNumber from the entity
       */
       @Column(name = "SEQ_NUMBER", length = 22, nullable = false)
       public Long getSeqNumber() {
       return this.seqNumber;
       }
      
       /**
       * Sets the SeqNumber of the entity
       */
       public void setSeqNumber(Long seqNumber) {
       this.seqNumber = seqNumber;
       }
      
       private String address = null;
      
       private String addressStandardized = null;
      
       private Long areaId = null;
      
       private Date lastUpdate = null;
      
       private Long locationId = null;
      
       private Long seqNumber = null;
      
       private Long userKey = null;
      }

      PK Class
      package com.company.project.entity;
      
      import java.io.Serializable;
      
      public class StreetWatchPK implements Serializable{
       private static final long serialVersionUID = 3355389391459690610L;
      
       private Long userKey = null;
      
       private Long locationId = null;
      
       private String addressStandardized = null;
      
       private Long areaId = null;
      
       /**
       * No-arg constructor
       */
       public StreetWatchPK( ) {}
      
       /**
       * All-fields constructor
       * @param userKey
       * @param locationId
       * @param addressStandardized
       * @param areaId
       */
       public StreetWatchPK(Long userKey, Long locationId, String addressStandardized, Long areaId) {
       this.userKey = userKey;
       this.locationId = locationId;
       this.addressStandardized = addressStandardized;
       this.areaId = areaId;
       }
      
       public boolean equals(Object obj) {
       if (obj == this)
       return true;
       if (!(obj instanceof StreetWatchPK))
       return false;
       StreetWatchPK pk = (StreetWatchPK) obj;
      
       //User Key
       if(locationId.longValue() != pk.getLocationId().longValue()){
       return false;
       }
       //Location Id
       if(userKey.longValue() != pk.getUserKey().longValue()){
       return false;
       }
       //Area Id
       if(areaId.longValue() != pk.getAreaId().longValue()){
       return false;
       }
       if (!addressStandardized.equals(pk.addressStandardized)){
       return false;
       }
       return true;
       }
      
       public int hashCode() {
       return addressStandardized.hashCode() +
       (int)areaId.longValue() +
       (int)locationId.longValue() +
       (int)userKey.longValue();
       }
      
       /**
       * @return the addressStandardized
       */
       public String getAddressStandardized() {
       return addressStandardized;
       }
      
       /**
       * @param addressStandardized the addressStandardized to set
       */
       public void setAddressStandardized(String addressStandardized) {
       this.addressStandardized = addressStandardized;
       }
      
       /**
       * @return the areaId
       */
       public Long getAreaId() {
       return areaId;
       }
      
       /**
       * @param areaId the areaId to set
       */
       public void setAreaId(Long areaId) {
       this.areaId = areaId;
       }
      
       /**
       * @return the locationId
       */
       public Long getLocationId() {
       return locationId;
       }
      
       /**
       * @param locationId the locationId to set
       */
       public void setLocationId(Long locationId) {
       this.locationId = locationId;
       }
      
       /**
       * @return the userKey
       */
       public Long getUserKey() {
       return userKey;
       }
      
       /**
       * @param userKey the userKey to set
       */
       public void setUserKey(Long userKey) {
       this.userKey = userKey;
       }
      
      }


        • 1. Re: Wrong query generated
          monkeyden

          So, any clues as to why a query would be generated with half Entity bean fields (userKey, locationId, areaId, addressStandardized) and half database fields (SEQ_NUMBER, LAST_UPDATE, ADDRESS)?

          • 2. Re: Wrong query generated
            monkeyden

            I'm sure my answer lies in the fact that the primary key fields are the only entity fields in the query...the rest are DB fields. I'm just not sure what to do about it. I'm reading (and have read) Bill's book, but the section on composite keys doesn't make any mention of how the EntityManager works with them beyond their definition.

            • 3. Re: Wrong query generated
              monkeyden

              I also understand that JBoss 4.0.4 doesn't implement the EJB spec correctly/completely. Does anyone have an example of a successfully implemented IdClass for this version? I'm using jems-installer-1.2.0.BETA2.jar.

              • 4. Mystery solved!
                monkeyden

                Mystery solved!

                The @Column annotations for the primary key fields don't go on the entity bean at all. They go on the methods in the @IdClass only, though it doesn't break if they are in both places.

                I now know that Bill refrained from the @Column annotations for clarity of the example, but beware of this in the book. Code below. Hope this helps someone.

                Entity

                package com.company.project.entity;
                
                import java.util.Date;
                
                import javax.persistence.Column;
                import javax.persistence.Entity;
                import javax.persistence.Id;
                import javax.persistence.IdClass;
                import javax.persistence.Table;
                
                import org.jboss.seam.annotations.Name;
                @Entity
                @Name("streetWatchEntity")
                @Table(name = "DWL_STREET_WATCH_LIST")
                @IdClass(StreetWatchPK.class)
                public class StreetWatchEntity implements java.io.Serializable {
                 private static final long serialVersionUID = 4855048029589330487L;
                
                 /**
                 * PRIMARY KEY FIELD
                 * Retrieves the UserKey from the entity
                 */
                 @Id
                 public Long getUserKey() {
                 return this.userKey;
                 }
                
                 /**
                 * Sets the UserKey of the entity
                 */
                 public void setUserKey(Long userKey) {
                 this.userKey = userKey;
                 }
                
                 /**
                 * PRIMARY KEY FIELD
                 * Retrieves the LocationId from the entity
                 */
                 @Id
                 public Long getLocationId() {
                 return this.locationId;
                 }
                
                 /**
                 * Sets the LocationId of the entity
                 */
                 public void setLocationId(Long locationId) {
                 this.locationId = locationId;
                 }
                
                 /**
                 * PRIMARY KEY FIELD
                 * Retrieves the AreaId from the entity
                 */
                 @Id
                 public Integer getAreaId() {
                 return this.areaId;
                 }
                
                 /**
                 * Sets the AreaId of the entity
                 */
                 public void setAreaId(Integer areaId) {
                 this.areaId = areaId;
                 }
                
                 /**
                 * PRIMARY KEY FIELD
                 * Retrieves the AddressStandardized from the entity
                 */
                 @Id
                 public String getAddressStandardized() {
                 return this.addressStandardized;
                 }
                
                 /**
                 * Sets the AddressStandardized of the entity
                 */
                 public void setAddressStandardized(String addressStandardized) {
                 this.addressStandardized = addressStandardized;
                 }
                
                 /**
                 * Retrieves the Address from the entity
                 */
                 @Column(name = "ADDRESS", length = 128, nullable = true)
                 public String getAddress() {
                 return this.address;
                 }
                
                 /**
                 * Sets the Address of the entity
                 */
                 public void setAddress(String address) {
                 this.address = address;
                 }
                
                 /**
                 * Retrieves the LastUpdate from the entity
                 */
                 @Column(name = "LAST_UPDATE", length = 7, nullable = true)
                 public Date getLastUpdateDate() {
                 return this.lastUpdateDate;
                 }
                
                 /**
                 * Sets the LastUpdate of the entity
                 */
                 public void setLastUpdateDate(Date lastUpdate) {
                 this.lastUpdateDate = lastUpdate;
                 }
                
                 /**
                 * Retrieves the SeqNumber from the entity
                 */
                 @Column(name = "SEQ_NUMBER", length = 22, nullable = false)
                 public Long getSeqNumber() {
                 return this.seqNumber;
                 }
                
                 /**
                 * Sets the SeqNumber of the entity
                 */
                 public void setSeqNumber(Long seqNumber) {
                 this.seqNumber = seqNumber;
                 }
                 private String address = null;
                 private String addressStandardized = null;
                 private Integer areaId = null;
                 private Date lastUpdateDate = null;
                 private Long locationId = null;
                 private Long seqNumber = null;
                 private Long userKey = null;
                }

                IdClass
                package com.company.project.entity;
                
                import java.io.Serializable;
                
                import javax.persistence.Column;
                
                public class StreetWatchPK implements Serializable{
                 private static final long serialVersionUID = 3355389391459690610L;
                
                 private String addressStandardized = null;
                
                 private Integer areaId = null;
                
                 private Long locationId = null;
                
                 private Long userKey = null;
                
                 /**
                 * No-arg constructor
                 */
                 public StreetWatchPK() {}
                
                 public StreetWatchPK(Long userKey, Long locationId, String addressStandardized, Integer areaId) {
                 this.userKey = userKey;
                 this.locationId = locationId;
                 this.addressStandardized = addressStandardized;
                 this.areaId = areaId;
                 }
                
                 @Override
                 public boolean equals(Object obj) {
                 if (obj == this)
                 return true;
                 if (!(obj instanceof StreetWatchPK))
                 return false;
                 StreetWatchPK pk = (StreetWatchPK) obj;
                
                 //User Key
                 if(locationId.longValue() != pk.getLocationId().longValue()){
                 return false;
                 }
                 //Location Id
                 if(userKey.longValue() != pk.getUserKey().longValue()){
                 return false;
                 }
                 //Area Id
                 if(areaId.intValue() != pk.getAreaId().intValue()){
                 return false;
                 }
                 if (!addressStandardized.equals(pk.getAddressStandardized())){
                 return false;
                 }
                 return true;
                 }
                
                 @Override
                 public int hashCode() {
                 return addressStandardized.hashCode() +
                 (int)areaId.intValue() +
                 (int)locationId.longValue() +
                 (int)userKey.longValue();
                 }
                
                 /**
                 * @return the addressStandardized
                 */
                 @Column(name = "ADDRESS_STANDARDIZED", length = 128, nullable = false)
                 public String getAddressStandardized() {
                 return addressStandardized;
                 }
                
                 /**
                 * @param addressStandardized the addressStandardized to set
                 */
                 public void setAddressStandardized(String addressStandardized) {
                 this.addressStandardized = addressStandardized;
                 }
                
                 /**
                 * @return the areaId
                 */
                 @Column(name = "AREA_ID", length = 22, nullable = false)
                 public Integer getAreaId() {
                 return areaId;
                 }
                
                 /**
                 * @param areaId the areaId to set
                 */
                 public void setAreaId(Integer areaId) {
                 this.areaId = areaId;
                 }
                
                 /**
                 * @return the locationId
                 */
                 @Column(name = "LOCATION_ID", length = 22, nullable = false)
                 public Long getLocationId() {
                 return locationId;
                 }
                
                 /**
                 * @param locationId the locationId to set
                 */
                 public void setLocationId(Long locationId) {
                 this.locationId = locationId;
                 }
                
                 /**
                 * @return the userKey
                 */
                 @Column(name = "USER_KEY", length = 22, nullable = false)
                 public Long getUserKey() {
                 return userKey;
                 }
                
                 /**
                 * @param userKey the userKey to set
                 */
                 public void setUserKey(Long userKey) {
                 this.userKey = userKey;
                 }
                
                }