-->
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: HQL and session level cache
PostPosted: Fri May 07, 2010 1:49 am 
Newbie

Joined: Fri May 07, 2010 1:25 am
Posts: 1
Hi all,

While working in hibernate session level cache and HQL i came across an inconsistent behavior

I have tried the following scenarios :

Case 1
Query q = s.createQuery("select new sample(s.a, s.b) from sample s").setCacheable(true);
List l = q.list();
sample s1 = (sample)s.get(sample.class, "abcd");
System.out.println(s1.getB());

Case 2
Query q = s.createQuery("from sample s").setCacheable(true);
List l = q.list();
sample s1 = (sample)s.get(sample.class, "abcd");
System.out.println(s1.getB());


sample.hbm.xml


<?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="sample" table="sample">

<id name="a" column="a" type="string" />
<property name="b" column="b" />
</class>
</hibernate-mapping>

sample.java

class sample {
private String a;
private int b;

public sample(String a) {
this.a = a;

}

public sample(String a, int b) {
this.a = a;

}
public sample() {

}
public String getA() {
return a;
}
public int getB() {
return b;
}
public void setA(String i) {
a = i ;
}
public void setB(int j) {
b = j;
}

public boolean equals(Object other) {
System.out.println("equals");
return true;
}

public int hashCode() {
System.out.println("hashCode");
return a.hashCode();
}
}
In the second case only one SQL query gets fired so the sample object is getting stored in the session cache, but in the first case two queries are getting fired so why hibernate not able to find the sample object in session cache returned from the query in case 1?

I have overriden equals and hashCode method in the POJO for sample class and i have noticed its never getting called in both the cases. Doesnt hibernate use these methods to lookup object in the cache

Regards
Prasanna


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.