-->
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.  [ 3 posts ] 
Author Message
 Post subject: Can you help me to clear a doubt about second level cache?
PostPosted: Wed Aug 13, 2008 1:18 pm 
Newbie

Joined: Wed Aug 13, 2008 12:48 pm
Posts: 7
Location: Mérida, Yucatán, México
Hibernate version: 3.2.5 ga
Ehcache version: 1.5

Hi!

We suppose that object's Person store in Second Level cache with Ehcache, with the next in Person.hbm.xml:


Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
   <class name="test.Person" table="person">
      <cache usage="read-only"/>
      <id name="id" column="id" type="integer">
         <generator class="identity"/>
      </id>
      <property name="name" column="name" type="string"/>
   </class>
</hibernate-mapping>


and the next in Hibernate.cfg.xml

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/testEhcache</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
           
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
       
        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

        <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

        <property name="hibernate.cglib.use_reflection_optimizer">true</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.default_batch_fetch_size">8</property>
        <property name="hibernate.query.substitutions">true 'T', false 'F', current_date current</property>

          <property name="hibernate.cache.use_query_cache">true</property>
          
        <mapping resource="test/Person.hbm.xml"/>
       
    </session-factory>
</hibernate-configuration>


Now, in a first Session, We get the Person with id = 1

Code:
Person p1 = session1.get(Person.class, new Integer(1));


and in a second session, we get the Person with id = 1 again

Code:
Person p2 = session2.get(Person.class, new Integer(1));


after we compare p1 and p2

Code:
if(p1 == p2)
   System.out.println("equals");
else
   System.out.println("No equals");


and the result is "No equals"

Now, We can see that p1 is not the same object that p2, but p1 has the same information that p2.

that´s rigth?
there is a way for Hibernate return us the same instance that is store in Ehcache each we get it in different Hibernate sessions?

Second level cache work's correctly because the first session query to db and the second session don't.

thank's a lot of for your help ;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 13, 2008 1:59 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
This is expected behavior. Two sessions are guaranteed to return different objects. The second-level cache doesn't cache the full objects, but only the individual property values that are part of the entities.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 12:18 pm 
Newbie

Joined: Wed Aug 13, 2008 12:48 pm
Posts: 7
Location: Mérida, Yucatán, México
thank's a lot of for your answer nordborg.

I maked tests for check your comment and you are right...

Other question...

how do I use the mvc pattern with hibernate objects if hibernate returns different objects for different sessions?


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