-->
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.  [ 1 post ] 
Author Message
 Post subject: Problem with read-write cache
PostPosted: Tue Mar 04, 2008 12:29 pm 
Newbie

Joined: Mon Jan 09, 2006 1:18 pm
Posts: 4
Hibernate version:
Code:
3.2.6


Name and version of the database you are using:
Code:
MySQL 5.0.51a-community-nt


Application server and server-side technology:
Code:
Tomcat 5.5.25
Struts 1.3.8


Problem:
Code:
I have a one-to-many relationship between Parent and Child tables. I have read-write caching (EHCache) and query caching enabled, and I have defined caching in both Parent.hbm.xml and Child.hbm.xml as well as the Child set in Parent.hbm.xml.

Running queries works great and by following the output from show_sql I can see that the data gets cached ok and table reads are eliminated.

The problem I am having is when I create a new Child. First I load an existing Parent object and close the session. Next I create a new Child object instance and set the Parent object using the Child's setParent() method. Next I call HibernateUtil.saveOrUpdate() to save the Child.

Once this is done I can query the database and see that all of the data was saved ok. But when I re-load that Parent using hibernate, the change is not visible. So I cannot see the Child attached to the Parent.

Can't figure this one out. Any ideas?


From hibernate.cfg.xml:
Code:
<property name="hibernate.transaction.flush_before_completion">true</property>
<property name="hibernate.transaction.auto_close_session">true</property>
<property name="hibernate.connection.release_mode">after_transaction</property>
<property name="jdbc.use_streams_for_binary">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>


From HibernateUtil:
Code:
public static void saveOrUpdate(Object obj) throws HibernateException {   
   Transaction = null;   
   Session session = null;   
   try {     
      session = HibernateUtil.currentSession();     
      tx = session.beginTransaction();     
      session.saveOrUpdate(obj);     
      tx.commit();
   } catch


From ehcache.xml:
Code:
<ehcache>
   <diskStore path="user.dir"/>
   <defaultCache
      maxElementsInMemory="500"
      eternal="false"
      timeToIdleSeconds="0"
      timeToLiveSeconds="0"
      overflowToDisk="false"
   />
</ehcache>



From Parent.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
   
<hibernate-mapping>

<class
    name="com.mycompany.Parent"
    table="parent"
>
    <cache usage="read-write"/>

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

    <version name="version" column="version"/>

    <property
        name="name"
        type="java.lang.String"
        column="name"
        not-null="true"
        length="100"
    />

    <!-- Associations -->

    <!-- bi-directional one-to-many association to Child -->
    <set
        name="children"
        lazy="false"
        inverse="true"
   cascade="none"
        sort="com.mycompany.comparator.ChildComparator"
    >
        <cache usage="read-write"/>
        <key>
            <column name="child" />
        </key>
        <one-to-many
            class="com.mycompany.Child"
        />
    </set>

</class>
</hibernate-mapping>


From action class where I am trying to add a new child:
Code:
Parent parent = ParentDAO.findById("123");

Child child = new Child();
child.setName("a new child");
child.setParent(parent);

HibernateUtil.saveOrUpdate(child);


From ParentDAO:
Code:
public static Parent findById(String id) {
   String param1 = id;
   String hql = "from com.mycompany.Parent as parent where parent.id = :param1";
   Session session = HibernateUtil.currentSession();
   session.beginTransaction();
   Query query = session.createQuery(hql);
   query.setCacheable(true);
   query.setString("param1", param1);
   List result = query.list();
   session.getTransaction().commit();

   Parent parent = null;
   if (result != null && !result.isEmpty()) {
      parent = (Parent)result.get(0);
   }

   return parent;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.