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: Lazy Loading/foreign generator and Caching Help
PostPosted: Wed Sep 12, 2007 7:31 pm 
Newbie

Joined: Wed Jul 25, 2007 2:16 pm
Posts: 8
I've got an issue with fields being cached in my application and i need to figure out how to dump that cache and get the value from the database. I've simplified my mappings for this post to get to the point. The "DetailText" class is used to separate ntext fields so that they load more efficiently due to their size. The problem is in my application, i update one of the fields (Description), and it does not update until either some cache expires or i touch the web.config to reset the web application. it would be nice to be able to just clear the nhibernate cache or whatever is going on. any ideas what i should do?

Code:
<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
                   namespace="Com.WildTangent.ProductCatalog.Data"
                   assembly="ProductCatalog">
    <class name="ProductLocale" table="t_ProductLocale" lazy="true">
        <cache usage="read-write" />

        <id name="Id" column="ProductLocaleId" type="int">
            <generator class="native" />
        </id>

        <property name="DisplayName" type="String" length="64" column="DisplayName" not-null="false" />

        <one-to-one name="DetailText" class="ProductDetailText" constrained="true" />

    </class>
</hibernate-mapping>


Code:
public class ProductLocale
{
        public virtual int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public virtual string DisplayName
        {
            get { return _displayName; }
            set { _displayName = value; }
        }

        public virtual string Description
        {
            get { return _detailText != null ? _detailText.Description : null; }
        }

        protected string _displayName;
        protected ProductDetailText _detailText;
}


Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
                   namespace="Com.WildTangent.ProductCatalog.Data"
                   assembly="ProductCatalog">
    <class name="ProductDetailText" table="t_ProductLocale" lazy="true">
        <cache usage="read-write" />

        <id name="Id" column="ProductLocaleId" type="int">
            <generator class="foreign">
                <param name="property">ProductLocale</param>
            </generator>
        </id>

        <!-- Note that NHibernate needs ntext fields to be mapped -->
        <!-- as StringClob - if you map them as string, they'll be -->
        <!-- truncated since the SQL will treat them as varchars. -->
        <property name="Description" column="Description" type="StringClob" />

        <property name="EditorReview" column="EditorReview" type="StringClob" />

        <property name="Story" column="Story" type="StringClob" />

        <one-to-one name="ProductLocale" class="ProductLocale" />

    </class>
</hibernate-mapping>


Code:
public class ProductDetailText
    {
        public virtual int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public virtual ProductLocale ProductLocale
        {
            get { return _productLocale; }
            set { _productLocale = value; }
        }

        public virtual string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        public virtual string EditorReview
        {
            get { return _editorReview; }
            set { _editorReview = value; }
        }

        public virtual string Story
        {
            get { return _story; }
            set { _story = value; }
        }

        public override bool Equals(object obj)
        {
            ProductDetailText other = obj as ProductDetailText;
            return other != null ? (
                other.Description == Description &&
                other.EditorReview == EditorReview &&
                other.Story == Story) : false;
        }

        public override int GetHashCode()
        {
            return typeof(ProductDetailText).GetHashCode() ^
                (Description != null ? Description.GetHashCode() : 0) ^
                (EditorReview != null ? EditorReview.GetHashCode() : 0) ^
                (Story != null ? Story.GetHashCode() : 0);
        }

        private int _id;
        private string _description;
        private string _editorReview;
        private string _story;
        private ProductLocale _productLocale;
    }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 12, 2007 8:15 pm 
Newbie

Joined: Wed Jul 25, 2007 2:16 pm
Posts: 8
after further investigation, i'm pretty sure this isn't a cache issue. the fields in the DetailText class are not updated until i reset the app. i tried doing a Session.Clear(), Evict() the object. there's nothing in the cache for sure.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 13, 2007 10:32 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hi,

I don't understand what you mean by "the fields in the DetailText class are not updated until i reset the app" ? Who should update those fields ? What are you doing to get those fields updated ?

Can you post a piece of code where you are:
a) instatiating a DetailText object
b) updating the object
c) saving it
d) retrieving it again

Is it when you're retrieving it again that you notice that the fields of the DetailText class are still having the same values as before you updated and saved your object ?

_________________
Please rate this post if it helped.

X.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 13, 2007 5:56 pm 
Newbie

Joined: Wed Jul 25, 2007 2:16 pm
Posts: 8
ok i'll try to be more clear. this is an EXTREMELY complex database, so i'll try to simplify a little.

here's how i'm accessing the value:

foreach (ProductLocale productLocale in product.ProductLocales.Values) {
string description = productLocale.DetailText.Description;
}

pretend the database gets updated directly after i've loaded Description in code. the next time the code path comes across productLocale.DetailText.Description. it will not be updated. i've tried Session.Clear(), Session.Evict(object).. she no worky.

i don't think i can provide any more info than that honestly. if it's not enough then don't bother with this :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 14, 2007 2:38 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hello,

If I understand you correctly, you're expecting NHibernate to go to the database every time you're accessing a property ? That is not the case: if you're working with lazy-loaded objects, this will be true for the first time you're accessing a method/property but after that, the object will not be sync'ed automatically with the DB.

If you want to retrieve the actual values you might:
a) refetch the object f.e. session.Get()
b) use Session.Refresh() to force NHibernate to refresh the object's values.

_________________
Please rate this post if it helped.

X.


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.