-->
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.  [ 5 posts ] 
Author Message
 Post subject: Saving and updating collections.
PostPosted: Thu Sep 09, 2004 4:20 am 
Beginner
Beginner

Joined: Tue Jan 27, 2004 8:25 am
Posts: 45
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 classes

Code:

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!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 09, 2004 6:07 am 
Beginner
Beginner

Joined: Tue Jan 27, 2004 8:25 am
Posts: 45
Can anyone help me please?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 09, 2004 8:27 am 
Newbie

Joined: Thu Jun 24, 2004 2:19 am
Posts: 10
there are less than two hours between your posts!!
did you check the table in the database and the discount rooms are not there or is just the "hotelRoom" Fk null ?

perhaps remove the inverse="true" from the set mapping.
http://www.hibernate.org/155.html

and i guess the dot in ".HotelRoomWithChildrenDisscount" is just a typo ssince you stripped the package names ?


cheers

pascal


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 09, 2004 8:44 am 
Beginner
Beginner

Joined: Tue Jan 27, 2004 8:25 am
Posts: 45
Thanks pascal for your reply.
Yes I checked the table and the row was not inserted. And I have also tried with inverse=false, but same happened...
maybe it has to be something with de composite id, or cache, I don't really know..... heeeeeeeeeeeeeelp!!!!!!!
cheers!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 09, 2004 10:36 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
this cannot be a cache problem
about composite id, you must have a good implementation of equals & hashcode.
about inverse=true (or false), understand that the inverse end of a bidirectionnal association does not manage the fk...

and use your debugger to check the ids and your java model (see if both end are ok)

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


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