-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Map Collections via non-key field?
PostPosted: Wed Sep 03, 2008 10:28 am 
Newbie

Joined: Wed Sep 03, 2008 10:17 am
Posts: 4
Hibernate version: 3.1

Mapping documents:

Name and version of the database you are using: AS400/DB2

Hi, I have 3 classes/tables. One is an Order_Overview, which contains FKs to other tables. Another is Order_Detail, containing an FK to Order_OVerview, and Order Line Items (a one-to-many mapping from Order_Overview to Order_Detail) I have no problem mapping a collection of Order_Details to Order_Overview, as Order_Overviews PK is a FK in Order_Detail. My problem is mapping an association to the 3rd table, which is Shipping information for a given Order. Order_Overview contains a non-key column referencing Shipping's PK. What would the best approach be to map a set collection of Shipping class to Order_Overview, such that when I retrieve a set of Order_Overviews, I also get the Shipping instance? Does this require me not to use a set at all (as I just need the class instance). Is the <component> tag the way to go? I'd appreciate any insight. Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2008 11:32 am 
Newbie

Joined: Wed Sep 03, 2008 10:17 am
Posts: 4
Component didn't work as I had hoped. I was able to map shippingId column in OrderHeader, but only got the id, (because only the id is in OrderHeader)not all of the attributes of the class (they returned null). I'm wondering if this is something taht needs to be done in the BillTo mapping file? To be more clear of what is happening, I'll provide code:

Order_Overview Table (OrderHeader)
Code:
public class OrderHeader  implements java.io.Serializable {


    // Fields   

     private String orderType;
     private Long statusId;
     private Long orderNumber;
     private Long orderGenerated;
     private Long billToId;
     private Long shipToId;
     private Long storeId;
     private Long serviceId;
     private Set<OrderDetail> orderDetails = new HashSet<OrderDetail>();
     private BillTo billToDetails; 


    // Setters/Getters for Association Mappings
   
    public Set<OrderDetail> getOrderDetails() {
       return this.orderDetails;
    }
   
    public void setOrderDetails(Set<OrderDetail> orderDetails) {
       this.orderDetails = orderDetails;
    }
   
    public BillTo getBillToDetails() {
      return billToDetails;
   }


   public void setBillToDetails(BillTo billToDetails) {
      this.billToDetails = billToDetails;
   }

   public String getOrderType() {
        return this.orderType;
    }
   
    public void setOrderType(String orderType) {
        this.orderType = orderType;
    }

    public Long getStatusId() {
        return this.statusId;
    }
   
    public void setStatusId(Long statusId) {
        this.statusId = statusId;
    }

    public Long getOrderNumber() {
        return this.orderNumber;
    }
   
    public void setOrderNumber(Long orderNumber) {
        this.orderNumber = orderNumber;
    }

    public Long getOrderGenerated() {
        return this.orderGenerated;
    }
   
    public void setOrderGenerated(Long orderGenerated) {
        this.orderGenerated = orderGenerated;
    }

    public Long getBillToId() {
        return this.billToId;
    }
   
    public void setBillToId(Long billToId) {
        this.billToId = billToId;
    }

    public Long getShipToId() {
        return this.shipToId;
    }
   
    public void setShipToId(Long shipToId) {
        this.shipToId = shipToId;
    }

    public Long getStoreId() {
        return this.storeId;
    }
   
    public void setStoreId(Long storeId) {
        this.storeId = storeId;
    }

    public Long getServiceId() {
        return this.serviceId;
    }
   
    public void setServiceId(Long serviceId) {
        this.serviceId = serviceId;
    }
   



   public boolean equals(Object other) {
         ....
   }
   
   public int hashCode() {
       .....
   }   
}


BillTo Table
Code:
public class BillTo implements java.io.Serializable{

   // Fields
     private Long billToId;
     private String address1;
     private String address2;
     private String address3;
     private String address4;
     private String address5;
     private String address6;
     private String zip;
     private String state;
     private String country;
     private String phone;
     private String email;
   
    // Property accessors
      public Long getBillToId() {
      return billToId;
   }

   public void setBillToId(Long billToId) {
      this.billToId = billToId;
   }

    public String getAddress1() {
        return this.address1;
    }
   
    public void setAddress1(String address1) {
        this.address1 = address1;
    }

    public String getAddress2() {
        return this.address2;
    }
   
    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    public String getAddress3() {
        return this.address3;
    }
   
    public void setAddress3(String address3) {
        this.address3 = address3;
    }

    public String getAddress4() {
        return this.address4;
    }
   
    public void setAddress4(String address4) {
        this.address4 = address4;
    }

    public String getAddress5() {
        return this.address5;
    }
   
    public void setAddress5(String address5) {
        this.address5 = address5;
    }

    public String getAddress6() {
        return this.address6;
    }
   
    public void setAddress6(String address6) {
        this.address6 = address6;
    }

    public String getZip() {
        return this.zip;
    }
   
    public void setZip(String zip) {
        this.zip = zip;
    }

    public String getState() {
        return this.state;
    }
   
    public void setState(String state) {
        this.state = state;
    }

    public String getCountry() {
        return this.country;
    }
   
    public void setCountry(String country) {
        this.country = country;
    }

    public String getPhone() {
        return this.phone;
    }
   
    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return this.email;
    }
   
    public void setEmail(String email) {
        this.email = email;
    }
}


Mapping file - OrderHeader.hbm.xml
Code:
<hibernate-mapping>
    <class name="OrderHeader">
      <id name="orderNumber" type="java.lang.Long">
         <column name="D0DOCO" precision="8" scale="0" />
        </id>
      <property name="orderType" type="java.lang.String">
         <column name="D0DCTO" length="2" />
      </property>
      <property name="statusId" type="java.lang.Long">
          <column name="D0STS" precision="5" scale="0" />
      </property>
      <property name="orderGenerated" type="java.lang.Long">
          <column name="D0TRDJ" precision="6" scale="0" />
      </property>
      <property name="shipToId" type="java.lang.Long">
          <column name="D0SHAN" precision="8" scale="0" />
      </property>
      <property name="storeId" type="java.lang.Long">
          <column name="D0URAB" precision="8" scale="0" />
      </property>
      <property name="serviceId" type="java.lang.Long">
          <column name="D0CMC1" precision="8" scale="0" />
      </property>
      <set name="orderDetails" inverse="true" lazy="false" cascade="all">
         <key column="D5DOCO" />
         <one-to-many class="OrderDetail" />
      </set>
      
      <component name="billToDetails" class="BillTo" lazy="true">
         <property name="billToId" column="D0AN8" />
      </component>
    </class>
</hibernate-mapping>


Mapping file - BillTo.hbm.xml
Code:
<hibernate-mapping>
    <class name="BillTo">
        <id name="billToId" type="java.lang.Long">
         <column name="D1AN8" precision="8" scale="0" />
      </id>
      <property name="address1" type="java.lang.String">
          <column name="D1ADD1" length="40" />
      </property>
      <property name="address2" type="java.lang.String">
          <column name="D1ADD2" length="40" />
      </property>
      <property name="address3" type="java.lang.String">
          <column name="D1ADD3" length="40" />
      </property>
      <property name="address4" type="java.lang.String">
          <column name="D1ADD4" length="40" />
      </property>
      <property name="address5" type="java.lang.String">
          <column name="D1ADD5" length="40" />
      </property>
      <property name="address6" type="java.lang.String">
          <column name="D1ADD6" length="40" />
      </property>
      <property name="zip" type="java.lang.String">
          <column name="D1ADDZ" length="12" />
      </property>
      <property name="state" type="java.lang.String">
          <column name="D1ADDS" length="3" />
      </property>
      <property name="country" type="java.lang.String">
          <column name="D1CTR" length="3" />
      </property>
      <property name="phone" type="java.lang.String">
          <column name="D1PH1" length="20" />
      </property>
      <property name="email" type="java.lang.String">
          <column name="D1EMAIL" length="40" />
      </property>
    </class>
</hibernate-mapping>


Really appreciate any insight into how to best retreive a BillTo instance using the billToId in OrderHeader table. Thank you.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2008 12:21 pm 
Newbie

Joined: Wed Sep 03, 2008 10:17 am
Posts: 4
I solved my own problem after doing some more searches. Appears I, as usual, was over-complicating the situation. I did read about the <many-to-one> mappings but it didn't click that it applied to my situation. I got rid of component, and did a <many-to-one> mapping from the FK column to BillTo class. Also, added lazy="false" attribute to the <many-to-one>. This returned the BillTo instance I needed!

Code:
<many-to-one name="billToDetails" class="BillTo" column="D0AN8" lazy="false" />


:)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.