-->
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.  [ 6 posts ] 
Author Message
 Post subject: How reload original collection from DB?
PostPosted: Thu Jun 15, 2006 12:04 pm 
Newbie

Joined: Fri Nov 11, 2005 6:09 am
Posts: 4
Location: Gomel, Belarus
I'm changing a collection of object. Next I want undo changes of the object and its collection. session.Refres for the object is not refresh collection!
How to refresh the objec and its collection(s)?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 15, 2006 12:39 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
this is a very vague question and needs more detail. post some code in your example. the answre could be a simple as calling some NH code or as complex as implementing the Memento Pattern...

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 16, 2006 5:43 am 
Newbie

Joined: Fri Nov 11, 2005 6:09 am
Posts: 4
Location: Gomel, Belarus
There are two classes. Class1 containa collection of Collection1Items.

public class Calss1
{
private IList m_Collection1 = new ArrayList();
public IList Collection1
{
get { return m_Collection1; }
set {m_Collection = value; }
}

...
}

public class CollectioItem1
{
private Class1 m_Class1;
public Class1 Class1
{
get { return m_Class1; }
set {m_Class1 = value; }
}

...
}

Mapping:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

<class name="Domain.Class1, Domain" table="CLASS1" dynamic-update="true" dynamic-insert="true">
<id name="Id" column="id" type="Int64" unsaved-value="0">
<generator class="Persistence.TableGeneratorUltra, Persistence">
<param name="table">unique_key</param>
<param name="id">class1_id</param>
</generator>
</id>
<bag name="Collection1" table="COLLECTION1ITEM" inverse="true" lazy="true" cascade="delete-orphan">
<key column="CLASS1_ID"/>
<one-to-many class="Domain.Collection1Item, Domain"/>
</bag>

...

</class>

<class name="Domain.Collection1Item, Domain" table="COLLECTION1ITEM" dynamic-update="true" dynamic-insert="true">
<id name="Id" column="id" type="Int64" unsaved-value="0">
<generator class="Persistence.TableGeneratorUltra, Persistence">
<param name="table">unique_key</param>
<param name="id">collection1item_id</param>
</generator>
</id>
<many-to-one name="Class1" column="CLASS1_ID" class="Domain.Class1, Domain" not-null="true"/>

...

</class>

</hibernate-mapping>

foo()
{
--open session...

Class1 class1 = session.Load(typeof(Class1), id);

--actions where lazy collection Collection1 is loading and takes place some changes of Colection1's and Class1' properties

if(true)
{
-- hire I want undo changes that was made above (I needn't save changes!)

session.Refresh(class1);

-- Method Refresh refreshed only class1 and its properties but NOT collection!

-- HOW CAN I REFRESH COLLECTION ???
}

--close session.
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 16, 2006 10:14 am 
Regular
Regular

Joined: Tue Jan 03, 2006 11:43 am
Posts: 51
Location: Sweden
I don't really know the answer to this question as I haven't used refresh, but I'll give it a try anyway..

First you can try is cascade="all-delete-orphan" instead of just "delete-orphan".
Otherwise you might have to do the refresh at the collection explicitly: session.Refresh(class1.Collection1)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 16, 2006 10:25 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
session.Refresh() isn't going to do it becaus the collection is lazily loaded. you are probably running into first level session cache issues-- second-level cache issues if you've got that enabled as well. so here are a few suggestions:

Initialize the child collection:

Code:
if(true)
{
    -- hire I want undo changes that was made above (I needn't save changes!)

    session.Refresh(class1);
    HibernateUtil.Initialize(class1.Collection1);
}

this isn't the best solution because you'll have to do the same for class1.Collection2, etc. another alternative is to:

Code:
if(true)
{
    -- hire I want undo changes that was made above (I needn't save changes!)

    session.Lock(class1, LockMode.Read);
}

this forces NH to sync the "in-memory" object with the database, reading the database and discarding the in-memory changes. however, this is more accurately used for reattaching an object to a new session. the next time you use your collection, it should load the collection from the db.

you could simply "reload" the object. this is probably your best bet:

Code:
if(true)
{
    -- hire I want undo changes that was made above (I needn't save changes!)

    session.Evict(class1); // or session.Clear()
    class1 = session.Load(typeof(Class1), id);
}

i don't think Memento would work here because you aren't supposed to replace an entire collection....

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 19, 2006 5:34 am 
Newbie

Joined: Fri Nov 11, 2005 6:09 am
Posts: 4
Location: Gomel, Belarus
Thanks, third case is sutable for my purpose.


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