-->
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.  [ 4 posts ] 
Author Message
 Post subject: .:: Newbie really desperate
PostPosted: Wed Mar 10, 2004 5:49 am 
Beginner
Beginner

Joined: Tue Jan 27, 2004 8:25 am
Posts: 45
Hi all!!

I've beeing searching in the forums for an answer to my problem, but I haven't found one, so finally I have decided to pos it here.

I'm having trouble in saving collections, but I don't really know if it is because of my mapping, my clasess, or whatever....

This is my scenario, ok?

This will be my parent class, which has a Set of children (HotelCostDetail)
Code:
public class HotelCostHead
{
   private Long code;
   private HColor displayColor;
   private HotelRoom hotelRoom;
   private Currency currency;
   private int rateType;
   private int basisIncluded;
   
   private VATType vatType;
   
   private Set costDetails;

// Follows setters and getters....
}


And this will be the child class, which has an composite ID, because the primary key of the table has 3 columns (hotelCostHead,numberOfAdults,numberOfChildren)...
Code:
public class HotelCostDetail
{
                private HotelCostHead hotelCostHead;
   private Long numberOfAdults;
   private Long numberOfChildren;
   private Double costValue;
   
   private HotelCostDetailID id;
// Follows setters and getters....
}


and here is the ID class for it

Code:

public class HotelCostDetailID implements Serializable
{
   private HotelCostHead hotelCostHead;
   private Long numberOfAdults;
   private Long numberOfChildren;

// Follows setters and getters....
public boolean equals( Object o )
   {
      if( o instanceof HotelCostDetail )
      {
         HotelCostDetail other = (HotelCostDetail) o;
         return (
               (this.hotelCostHead.equals(other.getHotelCostHead())) &&
               (this.numberOfAdults.equals(other.getNumberOfAdults())) &&
               (this.numberOfChildren.equals(other.getNumberOfChildren()))
         );
      }

      return false;
   }

   public int hashCode()
   {
      return hotelCostHead.hashCode() ^ numberOfAdults.hashCode() ^ numberOfChildren.hashCode();
   }
}


My first quyestion is: is this class really necessary? Or can I just make HotelCostDetail to extend Serializable and create the equals() and hashCode() methods??

So having this classes I have created the following mapping for hibernate:

Code:
<class name="HotelCostHead" table="HotelCostHead">
   <id name="code" type="long" column="code" unsaved-value="null">
      <generator class="native" />
    </id>
    <many-to-one name="displayColor" class="HColor" column="displayColor" unique="false"/>
    <many-to-one name="hotelRoom" class="HotelRoom" column="hotelRoom" unique="false"/>
    <many-to-one name="currency" class="Currency" column="currency"/>
    <property name="rateType" type="int"/>
    <property name="basisIncluded" type="int"/>
    <many-to-one name="vatType" class="VATType" column="vatType"/>
    <set name="costDetails" inverse="true" lazy="true" order-by="hotelCostCode" cascade="all">
       <key>
          <column name="hotelCostHead"/>
          <column name="numberOfAdults"/>
          <column name="numberOfChildren"/>
       </key>
        <one-to-many class="HotelCostDetail"/>
   </set>
</class>

<class name="HotelCostDetail" table="HotelCostDetail">
   <composite-id name="id" class="HotelCostDetailID">
      <key-many-to-one name="hotelCostHead" class="HotelCostHead" column="hotelCostHead"/>
      <key-property name="numberOfAdults" type="int"/>
      <key-property name="numberOfChildren" type="int"/>
   </composite-id>
   <property name="costValue" type="double" length="12"/>
</class>


Ok. So now imagine I want to insert a new HotelCostHead, with 3 or 4 new HotelCostDetails, so none of this objects exists in the database. How should the source code be for the save.
What I first do is I create a new HotelCostHead, assign the attributes, save it, and then save each of the children, but I gives me an error, saying that "the identifier for HotelCostDetail has changed from xxx to xxx", and I cann't save them...
Can anyone show an example of how doing this?

Thank's in advance!!!!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 11:21 am 
Beginner
Beginner

Joined: Tue Jan 27, 2004 8:25 am
Posts: 45
please help me......... :'(


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 11:25 am 
Beginner
Beginner

Joined: Tue Jan 27, 2004 8:33 am
Posts: 29
I think you should post the code you use to insert the objects


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 5:18 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
(o instanceof HotelCostDetail) in HotelCostDetailID class. This cannot work, beacause hibernate will give the equals() method a HotelCostDetailID object as per the java.lang.Object contract
Every proeprty inside <composite-id> should be in the HotelCostDetailID class.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.