-->
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.  [ 12 posts ] 
Author Message
 Post subject: How can I select records using <composite-id>?
PostPosted: Fri Jan 20, 2006 4:06 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Hi!

I'm using composite-id to map my old tables (nowadays I just use <id>).

So, I'd like to know how can I load records from the database using NHibernate and <composite-id>.

With a single key (using <id>) I've used this:

Code:
public static Atividade RetornaAtividade(int id)
{
        ISession session = NHibernateHelper.GetSession();
        return (Atividade)session.Get(typeof(Atividade), id);
}


In other words, I've used the method Get().

I have two questions:

- Has any other way to do the same above (using <id>)?

- And the main question: how can I load/select records using NHibernate and <composite-id>?

I'd like to use something like this:

Code:
Obj o = new Obj();
o.Key1 = "xxx";
o.Key2 = "yyy";

ISession.SomeMethod(o);


Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 3:47 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Any?

Please, i do need this :(

tks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 4:08 pm 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
Have You tried to use array? (just a wild guess...)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 6:41 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Well, just use one of the ISession.Load methods. If you don't have a special class for the composite identifier, you will have to create a dummy instance, fill in the key attributes, and pass it to Load.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 9:40 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
gert wrote:
Have You tried to use array? (just a wild guess...)


Could you make an example, please? I didn't understand :(

Tks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 9:44 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
sergey wrote:
Well, just use one of the ISession.Load methods. If you don't have a special class for the composite identifier, you will have to create a dummy instance, fill in the key attributes, and pass it to Load.


But the ISession.Load methods require one object or class + an Id, and I'm using a composite-id.

I've already used ISession.Load(obj, id) to a table$ mapped using just <id>.

I don't have a class for the composite id... do you recommend this? I'm a new user of nhibernate! =)

ANd without a class for the composite id, how should I do? Could you make an example, pls?

Tks :))


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 1:53 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
mdsrlz wrote:
gert wrote:
Have You tried to use array? (just a wild guess...)

Could you make an example, please? I didn't understand :(


I did mean something like
Code:
object[] id = new objec[2];
id[0] = ..;
id[1] = ..;
ISession.Get(typeof(..), id);


But as Sergey didn't mention this, it doesn't work propably


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 7:17 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Post your mapping file please.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 7:41 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
gert wrote:
Have You tried to use array? (just a wild guess...)


Hi!

This code doesn't work :(

Code:
object[] id = new object[2];

id[0] = value1;
id[1] = value2;

MyClass m = (MyClass) NHibernateHelper.GetSession().Get(typeof(MyClass), id);


The error message (at the Get method) is:

Quote:
identifier type mismatch
Parameter name: id


=(


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 7:48 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
sergey wrote:
Well, just use one of the ISession.Load methods. If you don't have a special class for the composite identifier, you will have to create a dummy instance, fill in the key attributes, and pass it to Load.

Sergey, THANKS! :~)

Now I understood... I've done this:

Code:
MyClass m = new MyClass();

m.Key1 = value1;
m.Key2 = value2;

NHibernateHelper.GetSession().Load(m, m);


Is it correct? It works fine! :~)

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 8:30 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Yes, I think that Load(typeof(MyClass), m) should work too and is preferable.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 10:46 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
sergey wrote:
Yes, I think that Load(typeof(MyClass), m) should work too and is preferable.


tkss ;o)))


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