-->
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.  [ 7 posts ] 
Author Message
 Post subject: session.load(Object o, Serializable s)
PostPosted: Thu Dec 04, 2003 4:06 pm 
Newbie

Joined: Thu Dec 04, 2003 3:45 pm
Posts: 9
Hello,

My table has a composed-id (a compozed primary key)!
My class has a method load(Long pk1, Long pk2) and I wanted to load this object, but I didn't find any way, hot to give a compozed id! Anyone any idea?

here my code:
Code:
   public void load(Long id1, Long id2) {
      Session session = null;
      AsyncRequest newObj = null;

      try {
         session = HibernateFactory.getSessionFactory().openSession();
     
         session.load(this, new Object[] { id1, id2 });// <-- HERE is the problem
      } catch (HibernateException e) {
         e.printStackTrace();
      } finally {
         try {
            session.close();
         } catch (HibernateException e) {}
      }
   }


I tried this way, but it doesn't work!!
Hope anyone can help me


Thanx in advance,
gimi


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 04, 2003 5:08 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I have never worked with composite-id so my question may seem idiotic :)

According to documentation, you have to specify class for <composite-id> element. Right? So you have to have special ID class. Why can't you just create an istance of this class, set both of its ID properties and use THIS instace as id in session.load? Doesn't it work?

I means something like

Code:
  MyId id = new MyId();
  id.setA(new Long(1));
  id.setB(new Long(2));

  session.load(id);


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 04, 2003 6:43 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
As dimas said
map you composite-id on a serializable class representig you composite id (see doc on this subject, equals and hashCode must be overriden)

Then use load(Object, Serializable).

If you don't want that, then do a plain HQL query .

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 12:36 pm 
Newbie

Joined: Thu Dec 04, 2003 3:45 pm
Posts: 9
epbernard wrote:
As dimas said
map you composite-id on a serializable class representig you composite id (see doc on this subject, equals and hashCode must be overriden)

Then use load(Object, Serializable).

If you don't want that, then do a plain HQL query .


The problem is solved! I created a new class for the PrimaryKey and it worked fine! The only thing maybe is, that first I tried to create a inner-class, but i got an error the class needs an empty constructor or something like this, although there was the constructor! The same class was correct as non inner-class!

Is this a bug??

anyway, thank you very much for your help

gimi


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 12:43 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I think inner class didn't work because you forget to make it "static" and that means it need to know reference to "outer" class instace (and this reference is passed to the constructor and that is why your 0-parameters constructor become 1-parameter constructor)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 12:44 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You need static inner class

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 1:06 pm 
Newbie

Joined: Thu Dec 04, 2003 3:45 pm
Posts: 9
Ups..

Didn't read carefully :-)
now it worked with inner class just great.. thanx

gimi


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