-->
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: How to retrieve class identifier
PostPosted: Mon May 30, 2005 2:21 pm 
Regular
Regular

Joined: Thu May 12, 2005 10:12 am
Posts: 71
Location: Buenos Aires, Argentina
This one should be easy for developers of nhibernate. I'm creating a generic DAO using nhibernate and got kinda stuck in DeleteById since I have to get the class identifier.

So basically I have to do


"from " + +

Code:
sessio.Delete("from " + type.Name + where " + someWhere.GetIdentifier(type) + " = " + id.ToString()


Is there a place in nhibernate for this "someWhere.GetIdentifier(type)" ?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 30, 2005 4:45 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
I believe you should be able to use lowercase 'id' as the property name - it's a special property that corresponds to the object's primary key.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 10:44 am 
Regular
Regular

Joined: Thu May 12, 2005 10:12 am
Posts: 71
Location: Buenos Aires, Argentina
No luck. Using underscore id in the query did not work thorwing an "Invalid column name 'id' " exception.

I'm sure there was a way to extract the identifier somehow.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 06, 2005 2:58 pm 
Regular
Regular

Joined: Thu May 12, 2005 10:12 am
Posts: 71
Location: Buenos Aires, Argentina
I finally got it right looking at the unit test (why didnt I do that first :oops: )

Anyways, if someone is reading this far is because your intrested in an answer so here goes the code on how to do a generic DeleteById

Code:
public void DeleteById(System.Type clazz, object id)
{
  try
{
         //the next line depends on how you obtain the ISession in your app
         ISession session = NHContext.Current.NHibernateSession;

   session.Delete("from o in class " + clazz.Name + " where o.id = " +  id.ToString());
  }
  catch (Exception ex)
  {
   //log error
   throw;
  }
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 07, 2005 7:11 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Or (more classic) :wink:

Code:
session.Delete( "from " + clazz.Name + " o where o.id = " +  id.ToString() );

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


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.