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!!!!