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
|