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 ;)