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.  [ 8 posts ] 
Author Message
 Post subject: Is it possible to delete by ID?
PostPosted: Fri Sep 14, 2007 12:23 pm 
Newbie

Joined: Wed Sep 05, 2007 1:25 am
Posts: 10
hi,
Kind of new to nHibernate.
I find a bit strange and not good for performance that in order to delete a record into database you have to retrieve the entity and then delete.2 network trips.

Can't we just delete based on the id ?
may be I am missing the obvious.

Can somebody help/explain?
Thanks


Top
 Profile  
 
 Post subject: Re: Is it possible to delete by ID?
PostPosted: Fri Sep 14, 2007 2:51 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
devnet wrote:
hi,
Kind of new to nHibernate.
I find a bit strange and not good for performance that in order to delete a record into database you have to retrieve the entity and then delete.2 network trips.

Can't we just delete based on the id ?
may be I am missing the obvious.

Can somebody help/explain?
Thanks


Yes, you could use an HQL query, something like:
"delete from myClass where myClass.ID = " + myClassId;

It will still require 2 trips. When you delete from NHibernate, then entities deleted will first be loaded into the cache. So, if you said something like:
"delete from myClass"
All your myClass instances in the database would have to be loaded into the cache first and then the delete would happen second.

Two calls for a delete isn't too bad, you are still using the same connection. If, however, you have alot of deletes, you can enlist ADOCommands into the current NHibernate transaction.
See - http://www.lostechies.com/blogs/joshua_ ... ction.aspx

This is the type of approach I take for batch operations. Just remember, when you enlist a command, the operations in the command are unmanaged and can screw up your session state. I think you'd have to call session.Refresh(entity) if a loaded entity was dependent on the command executed (such as deleted children). It would probably be safest to just call session.Flush() before executing the enlisted command and session.Clear() after.

How that helps.

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 15, 2007 1:45 am 
Newbie

Joined: Wed Sep 05, 2007 1:25 am
Posts: 10
Thank you very much for your reply.
However i think this is extremely bad for performance.
2 TRIPS TO MAKE A DELETE ???.

Also and this is because I am new to Nhibernate.That sometimes if you are not careful how you code it will make an insert instead of an update.
I believe that you have to use the same instantiation of your class otherwise it beleives its a new entity and perform an insert instead of an update.
Have you had it before?

Is it possible to retrieve things from the cache without having a network trip to the database?If so where can I find an example?
Thanks a lot


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 15, 2007 2:35 am 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
devnet wrote:
Thank you very much for your reply.
However i think this is extremely bad for performance.
2 TRIPS TO MAKE A DELETE ???.

Again, the entities are managed by NHibernate through the cash, so they must first be loaded then deleted. Enlisting a command for the delete wil bypass this.

devnet wrote:
Also and this is because I am new to Nhibernate.That sometimes if you are not careful how you code it will make an insert instead of an update.
I believe that you have to use the same instantiation of your class otherwise it beleives its a new entity and perform an insert instead of an update.
Have you had it before?

Right, if you create a new instance it is a different entity. NHibernate serves as your repository and you use it to manage persitable entities. If you have a new instance and the ID is not set yet, it's new and saving it will add it to the DB. You can put unique constraints in your mapping if you have properties that are unique given a particular class (without having to add constraint in database itself). I can't remember when I've accidentally inserted something last, but I've been using NHibernate fro about 3+ years now. Just remember, an entity is identified by it's reference, not it's attributes. If there's no ID, it's new...if you pulled it from the repository, it's a pre-existing object.

devnet wrote:
Is it possible to retrieve things from the cache without having a network trip to the database?If so where can I find an example?

Most definitely...in fact this is one of the great strengths of NHibernate. Once you insert an item or retrieve an item from the repository it is in the cash by default (unless you evict the object or clear the session). You should read about the difference of Get and Load. Also, you can set the session's FlushMode so that data is only flushed to the database on Commits if you like. Above all I'd recommend handling all saves, deletes, etc. in transactions and deal with well defined units of work.
Once you get the hang of it, you'll see that you can often reduce round trips thanks to the cache. I'd recommend setting up a log for NHibernate using log4net and setting it to debug. You can see exactly what's going on on the back end.

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 15, 2007 2:43 am 
Newbie

Joined: Wed Sep 05, 2007 1:25 am
Posts: 10
Fantastic replies.Thanks a lot

I guess I have to learn how to use the cache properly and understand the difference between get and load.I suppose one of the 2 doensnt make a trip to the db.

The problem is that the community of NHibernate is getting bigger but there are no books about it,where you can quickly learn these vital bits.
Or some good examples to download.
Thanks again


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 15, 2007 2:49 am 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
and don't forget to rate people's posts if they help you...some folks need the street cred (LOL).

BTW, have you used log4net before? It's been a major help for me with debugging.

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 15, 2007 2:51 am 
Newbie

Joined: Wed Sep 05, 2007 1:25 am
Posts: 10
I havent used log4dotnet yet.I will.
I will find out how you rate somebody post and do it.thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 15, 2007 3:12 am 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
In a nutshell here's how you do it. If you have any questions I'd be glad to help. I can't live without it and alot of times people will ask for logs when you seek help on the forums.

First off, reference (I do so by browse) log4net.dll, a copy is in the NHibernate bin.

In your code (say, at the beginning of a unit test, on load of a page, at the beginning of main, whatever) you'll call the log4net configurator

using log4net.Config;
...
XmlConfigurator.Configure();

You'll also need to set up your log4net configuration and NHibernate logger in either your web.config or app.config like this:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<!--...skipped NHibernate config in example...-->
    </configSections>
<!-- other log stuff here --->

    <log4net>
      <!-- file appender will write to a flat file named NHibernate-log.txt"-->
      <appender name="NHibernateFileAppender"   type="log4net.Appender.FileAppender">
         <file value="NHibernate-log.txt" />
         <appendToFile value="true" />
         <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
         </layout>
      </appender>

        <root>
            <level value="INFO" />
            <!-- putting appenders here will log all log traffic to appender targets -->
        </root>

        <!-- For NHibernate this is based on namespace -->
        <!-- Logger name NHibernate will capture all NHibernate messages -->
        <!-- You can use namespaces to split messages to different loggers using different appenders -->
        <logger name="NHibernate">
           <!-- DEBUG, INFO, WARN, ERROR...set to lowest level you want to log at -->
           <level value="DEBUG"/>
           <!-- references appender defined above for NHibernate log -->
           <appender-ref ref="NHibernateFileAppender" />
        </logger>
    </log4net>
</configuration>


I love log4net and use it alot in my own code, not just with NHibernate.

More info on configurations are here:
http://logging.apache.org/log4net/relea ... ation.html
And appenders here:
http://logging.apache.org/log4net/relea ... mples.html
Quick and dirty guide:
http://haacked.com/archive/2005/03/07/C ... tions.aspx

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


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