-->
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.  [ 4 posts ] 
Author Message
 Post subject: 3 little questions about NHibernate in a Detached scenario
PostPosted: Fri Jan 19, 2007 11:10 am 
Beginner
Beginner

Joined: Fri Jan 19, 2007 10:45 am
Posts: 23
Hello,
I'm using NHibernate 1.2.0 beta3.

I have 2 simples objects linked by a One-To-Many collection with the following mapping :
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="WindowsApplication1" assembly="WindowsApplication1">
<class name="Account" table="Account" select-before-update="true" dynamic-update="true">
<id name="Id">
<column name="AccountId" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex" />
</id>
<version name="Version"/>
<property name="NoCli" />
<property name="NoDos" />
<set name="Roles" lazy="false" cascade="all-delete-orphan" inverse="true">
<key column="AccountId" />
<one-to-many class="Role"/>
</set>
</class>
<class name="Role" table="Role" select-before-update="true" dynamic-update="true">
<id name="Id">
<column name="RoleId" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex" />
</id>
<version name="Version"/>
<property name="Property1" />
<property name="Property2" />
<many-to-one name="Account" column="AccountId" not-null="true"/>
</class>
</hibernate-mapping>


I instanciate and bind a list of Account using the folling commands :
ISession session = m_sessionFactory.OpenSession();
IQuery query = session.CreateQuery("select c from Account as c");
m_list = new BindingList<Account>(query.List<Account>());
bindingSource1.DataSource = m_list;
session.Close();

Then, I save my list of Account using the folling commands :
ISession session = m_sessionFactory.OpenSession();
ITransaction tx = session.BeginTransaction();
try
{
foreach (Account account in m_list)
{
session.SaveOrUpdate(account);
}
tx.Commit();
}
catch
{
tx.Rollback();
throw;
}
session.Close();

I have 3 questions :

First question, if I update a property of one of the object, when I save my list many times, it keeps sending the same SQL UPDATE order and incrementing each time the version of the corresponding object. I was expecting to have an update only the first time I save the list.

Second question, when I save the list, it send an SQL SELECT order for each object (Account and Role) in my list. I understand that NHibernate needs to read what is the current state in the database to decide if it's necessary or not to perform an update. Is it possible to use the Second Level cache instead of reading the database to know what is the current state ? I tried by adding the element <cache usage="read-write"/> in my mapping but it didn't change anything...

Third question, when I delete a role, and then save my list, Nhibernate knows that it need to generate a DELETE SQL order. How can it guess it ? It is not by reading the database... It is not by using the session... Is it because of the use of these special collections that implements ISet ?

Thanks for your help...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 22, 2007 5:22 am 
Beginner
Beginner

Joined: Fri Jan 19, 2007 10:45 am
Posts: 23
I did not received an answer to any of my 3 little questions...
Are they stupid questions ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 2:40 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
#1 - It is incrementing because you are issuing a save with the most current version number and are refreshing the object. If someone were to pull in the object with the same version number after you have already loaded the object and then you save your object, then their save would fail because they have an older version (is that pessimistic ?)
#2 - Make sure you have set up the 2nd level cache in the config and that you are using the right strategy...I believe read-write strategy will hit the DB in this scenario.
#3 - You are using cascade strategy all-delete-orphan for your collection, so when the Role is deleted NHibernate will issue a DELETE for the Role if it isn't in another collection. Read the docs about 'cascade' strategy.

Good luck
MIKE

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 5:56 pm 
Beginner
Beginner

Joined: Thu Dec 21, 2006 11:38 am
Posts: 30
BTW: Don't forget you can use the using statement on the transaction part:

Code:
using(ITransaction tx = session.BeginTransaction)
{
   
   tx.Commit();
} //will auto dispose/rollback if error.


Just makes the code a little more concise,

HTH a little :-)


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