Hi all!
I've been some days reading the book I bought, and reading the online documentation, but I'm having some trouble regarding collections. I really need some help to understand this, because I'm getting really frustrated.
I'm going to write an example of what I'm pretending to do.
Hibernate version:
2.1x
Mapping documents:
Code:
<class name="HotelRoom" dynamic-update="true">
<cache usage="read-write" />
<id name="code" type="long" column="code" unsaved-value="null">
<generator class="native" />
</id>
<property name="ord" type="long"/>
<property name="name" type="string" length="60"/>
<property name="ident" type="string" length="20"/>
<property name="description" type="string" length="60"/>
<many-to-one name="hotel" class="Hotel" column="hotel" unique="false"/>
<property name="minAdults" type="int"/>
<property name="maxAdults" type="int"/>
<set name="childrensDisscount" inverse="true" lazy="true" order-by="hotelRoom" cascade="all">
<key column="hotelRoom"/>
<one-to-many class="HotelRoomWithChildrenDisscount"/>
</set>
</class>
<class name=".HotelRoomWithChildrenDisscount" lazy="true">
<composite-id>
<key-many-to-one name="hotelRoom" class="HotelRoom" column="hotelRoom"/>
<key-property name="numAdults" type="int"/>
<key-property name="numChildren" type="int"/>
</composite-id>
</class>
Java classesCode:
public class HotelRoom
{
private Long code;
private long ord;
private String name;
private String ident;
private String description;
private Hotel hotel;
private int minAdults;
private int maxAdults;
private Set childrensDisscount = new HashSet();
public HotelRoom()
{
super();
}
// Getters and setters....
public void addNewHotelRoomWithChildrenDisscount(HotelRoomWithChildrenDisscount hRoomWithChDisscount)
{
hRoomWithChDisscount.setHotelRoom(this);
childrensDisscount.add(hRoomWithChDisscount);
}
}
public class HotelRoomWithChildrenDisscount implements Serializable
{
private HotelRoom hotelRoom;
private Integer numAdults;
private Integer numChildren;
// Getters and setters....
public boolean equals( Object o )
{
if( o instanceof HotelRoomWithChildrenDisscount )
{
HotelRoomWithChildrenDisscount other = (HotelRoomWithChildrenDisscount) o;
return (
(this.numAdults.equals(other.getNumAdults())) &&
(this.numChildren.equals(other.getNumChildren())) &&
(this.hotelRoom.equals(other.getHotelRoom())));
}
return false;
}
public int hashCode()
{
return numAdults.hashCode() ^ numChildren.hashCode() ^ hotelRoom.hashCode();
}
}
Code between sessionFactory.openSession() and session.close():Code:
Transaction tx = HibernateSession.currentSession().beginTransaction();
HotelRoom hotelRoom = new HotelRoom();
hotelRoom.setOrd(rq.getRoomOrd());
hotelRoom.setDescription(rq.getRoomDescription());
hotelRoom.setIdent(rq.getRoomIdent());
hotelRoom.setMaxAdults(rq.getRoomMaxAdults());
hotelRoom.setMinAdults(rq.getRoomMinAdults());
hotelRoom.setName(rq.getRoomName());
hotelRoom.setHotel(hotel);
for(int i=0;i<rq.getRoomChildDisscountArray().length;i++)
{
RoomChildDiss xmlRoomDisscount = (RoomChildDiss)rq.getRoomChildDisscountArray(i);
HotelRoomWithChildrenDisscount hRoomChildDiss = new HotelRoomWithChildrenDisscount();
hRoomChildDiss.setNumAdults(new Integer(xmlRoomDisscount.getNumberOfAdults()));
hRoomChildDiss.setNumChildren(new Integer(xmlRoomDisscount.getNumberOfChildren()));
hotelRoom.addNewHotelRoomWithChildrenDisscount(hRoomChildDiss);
}
HibernateSession.currentSession().save(hotelRoom);
tx.commit();
So, what I'm trying to do is to create a new hotelRoom, and save it. To this point there is no problem, but the collection is not saved in it's table.
So if for example this room has 3 childrens with disscount, the 3 children with disscount are not saved in their table.
What I'm doing wrong?
Do I have to save each HotelRoomWithChildrenDisscount first to make them persistent?
Any help would be very appreciated.
Thanks in advance for any response!