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.  [ 2 posts ] 
Author Message
 Post subject: What to do if a row gets deleted while I have it in cache?
PostPosted: Tue Feb 17, 2009 11:51 am 
Regular
Regular

Joined: Wed Feb 11, 2009 10:58 am
Posts: 55
Hello,

There's a problem I can't really get a solution for. Maybe somebody of you knows how to solve this.

Assume I have class containing a list (bag) of elements. While I edit the Data somebody deletes an entry from the list from the DB.
Now if I flush the object NHibernate tries to update the none-existing row, because he still has it in cache. Is there any possibility to say NHibernate what to do?

I think there are only 2 possibilities:

1. Ignore the update statement
2. Insert a new Listitem into the DB

The only way I could think of is to lock the rows for update, if I select them, but that would be a bit unhandy for my task.

Anybody got an idea? Thanks in advance!

Greetings,
Reflection


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 6:48 am 
Regular
Regular

Joined: Wed Feb 11, 2009 10:58 am
Posts: 55
OK, I built a workaround, though this is very ugly, but at least it
works as long as there is no other solution:

I built a helper class mapping to the same table which looks like
this:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="BookToolV2" namespace="BookTool">
  <class name="ChapterHelper" table="CHAPTER" lazy="true" >
    <id name="ChapterId" column="ID_CHAPTER" type="Decimal">
      <generator class="assigned" />
    </id>
    <property name="BookId" type="Decimal" column="ID_BOOK" />
    <property name="ChapterType" type="Decimal" not-null="true" column="CHAPTER_TYPE"/>
  </class>
</hibernate-mapping>


Code:
class ChapterHelper
  {
    public virtual Decimal ChapterHelperId { get; set; }
    public virtual Decimal BookId { get; set; }
    public virtual Decimal ChapterType { get; set; }



}



I use this in the onFlushDirty Event of the Interceptor on the main
Session:

Code:
class MyInterceptor: IInterceptor
{
...
public bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)
    {
      if (entity.GetType().IsSubclassOf(typeof(Chapter)))
      {
        Chapter chapter = (Chapter)entity;
        Chapter checkChapter;
        ISession checkSession = NHibernateFactory.CheckSession; // a second session


        checkChapter = (Chapter)checkSession.Get(typeof(Chapter), (Object)chapter.Id);


        if (checkChapter == null)
        {
          DialogResult result = MessageBox.Show("Instance was not found in DB!");
          ChapterHelper helpchapter= new ChapterHelper();
          helpchapter.ChapterHelperId =chapter.Id;
          helpchapter.BookId = chapter.BookId;
          helpchapter.ChapterType = chapter.GetStypeTypeId(); // in the main mapping file this is a discriminator


          checkSession.Save(helpchapter);
          checkSession.Flush();
        }
      }


      return false;
    }
...
}


I hope to find a better solution, because this is really really ugly = (

Any advice would be great!!!


Thank you! [/code]


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