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 detach objects
PostPosted: Tue Sep 02, 2008 7:33 am 
Newbie

Joined: Fri Jan 05, 2007 5:36 am
Posts: 16
How can I retrieve objects with NHibernate and in some way make them readonly or detach them from the session?



Cheers,
Yagami


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2008 8:39 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
You can mark readonly entities with mutable="false" in your mapping.

You can remove an entity from the session with session.Evict()


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2008 9:05 am 
Newbie

Joined: Fri Jan 05, 2007 5:36 am
Posts: 16
cremor wrote:
You can mark readonly entities with mutable="false" in your mapping.

You can remove an entity from the session with session.Evict()


I tried mutable="false" which would have been the most fitting solution but it didn't help at all. Will try to find something in the documentation about it.

Will try session.Evict() did actually work really nice after settings cascade="all-delete-orphan". I would really like to use this in a more global way. I guess I create an ReadOnlyNHibernateRepository. :)

How does FlushMode.Never works?

My problem is that I try to retrieve an object from the database, work with it and do some changes to it and just not save the changes to the database.

I pass an SessionProvider that implements an ISessionProvider interface to my repository that provides me with sessions to work with.
First I used my normal session provider and then I thought I could make something like this but it didn't work out too well.

Code:

From my NHibernateManager:

public ISession GetReadOnlySession
{
    get
    {
        ISession session = GetFactory.OpenSession(GetSession.Connection);
        session.FlushMode = FlushMode.Never;
        return session;
    }
}

Destructor in ReadOnlyNHibernateSessionProvider:

~ReadOnlyNHibernateSessionProvider()
{
    NHibernateManager.GetInstance.GetReadOnlySession.Dispose();
}

public ISession GetCurrentSession
{
    get
    {
        return NHibernateManager.GetInstance.GetReadOnlySession;
    }
}



Any thoughts about the code? :)




Cheers,
Yagami


Last edited by yagami on Tue Sep 02, 2008 10:13 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2008 9:55 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
As far as I know mutable="false" don't set your entities immutable but instead you say NHibernate that they are immutable. So NHibernate can optimize some internal things.

I've never used session.Evict() myself but as far as I understand it, it removes the entity from the session so the session won't send any SQL satements for that entity to the database. If it's not working that way I must be wrong ;-)

See that thead for some info about FlushMode.Never:
http://forum.hibernate.org/viewtopic.php?t=989502


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2008 10:22 am 
Newbie

Joined: Fri Jan 05, 2007 5:36 am
Posts: 16
cremor wrote:
As far as I know mutable="false" don't set your entities immutable but instead you say NHibernate that they are immutable. So NHibernate can optimize some internal things.

I've never used session.Evict() myself but as far as I understand it, it removes the entity from the session so the session won't send any SQL satements for that entity to the database. If it's not working that way I must be wrong ;-)

See that thead for some info about FlushMode.Never:
http://forum.hibernate.org/viewtopic.php?t=989502



Actually my session.Evict() did work after I changed the children to
cascade="all-delete-orphan". :)
Now is the only problem that I need it in a more global way to do this.

Will read more about FlushMode.Never now. Thanks a lot for your help!


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.