-->
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: SubSelect stale data
PostPosted: Mon Apr 14, 2008 11:02 am 
Newbie

Joined: Fri Mar 28, 2008 2:37 pm
Posts: 4
Location: Toronto
Hibernate version: 3.2.6.ga

Name and version of the database you are using: PostgreSQL Database Server 8.2.4

Debug level Hibernate log excerpt: no log4j

Hi, I'm trying to use subselect in my code, and I don't really understand what dose synchronize tag do.
I have read an issue about that in http://opensource.atlassian.com/project ... e/HHH-1568 , yet the last line there says
this is my last comment here since this is not a bug; feel free to start a talk in the forum and put the link here.
and since I couldn't find
any posting here, I decided to write one

here is a test case
I have one table called customer with 3 columns
Code:
CREATE TABLE customer
(
  id integer NOT NULL,
  name varchar2(100),
  type character(1) NOT NULL,
  age smallint,
  CONSTRAINT customer_pkey PRIMARY KEY (id)
)


and I have in it Customer7 id=7 name="Customer7" type="P" age="25"

I have 2 mapping documents

Mapping documents:

Code:
   <class
      name="CustomersView"
   >
   <subselect>
SELECT customer.id, customer.name, customer.age, CASE WHEN customer.type = 'P'  THEN 'Partner'  WHEN customer.type = 'I'  THEN 'Individual'  WHEN customer.type = 'C'  THEN 'Company'  ELSE 'Other'  END AS type FROM customer
   </subselect>
   <cache usage="read-write"/>
   <synchronize table="Customer"/>
      <id
         name="id"
         type="java.lang.Long"
         column="id"
      >
         <generator class="assigned"/>
      </id>
      <property
         name="name"
         column="name"
         type="string"
         not-null="false"
      />
      <property
         name="type"
         column="type"
         type="string"
         not-null="false"
      />
      <property
         name="age"
         column="age"
         type="integer"
         not-null="false"
         length="5"
      />
   </class>

Code:
<class
      name="Customer"
      table="customer"
   >
      <meta attribute="sync-DAO">true</meta>
      <id
         name="id"
         type="java.lang.Long"
         column="id"
      >
         <generator class="increment"/>
      </id>

      <property
         name="name"
         column="name"
         type="string"
         not-null="false"
         length="100"
      />
      <property
         name="type"
         column="type"
         type="string"
         not-null="true"
         length="1"
      />
      <property
         name="age"
         column="age"
         type="integer"
         not-null="false"
         length="5"
      />

   </class>


Now here is the issue, if I updated the class using Customer class, shouldn't the CustomersView cache
be updated since I'm using <synchronize table="customer"/>?

when I tried that, it did not happen
here is my code
Code:
package firstTest;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.Transaction;
import firstTest.base._BaseRootDAO;
import firstTest.dao.CustomerDAO;

public class TestViewCache {

   public static void main(String[] args) {
      _BaseRootDAO.initialize();
      Session s = new CustomerDAO().getSession();
      // get CustomersView for the first time
      Query q1 = s.createQuery("from CustomersView where id=7");
      q1.setCacheable(true);
      List res = q1.list();
      Transaction t = s.beginTransaction();
      // change Customer
      Query q2 = s.createQuery("from CustomerData where id=7");
      List<CustomerData> ls2 = q2.list();
      CustomerData customer = ls2.get(0);
      if (customer.getAge() == null) {
         customer.setAge(0);
      }
      customer.setAge(customer.getAge()+1);
      t.commit();
      // get CustomersView for second time
      res =  q1.list();
      _BaseRootDAO.closeCurrentSession();
   }
}


with hibernate.show_sql=true and log4j priorety="ERROR"
if we set q1.setCacheable(true) here is what we will get
Code:
Hibernate: select customersv0_.id as id5_, customersv0_.name as name5_, customersv0_.type as type5_, customersv0_.age as age5_ from (
SELECT customer.id, customer.name, customer.age, CASE WHEN customer.type = 'P'  THEN 'Partner'  WHEN customer.type = 'I'  THEN 'Individual'  WHEN customer.type = 'C'  THEN 'Company'  ELSE 'Other'  END AS type FROM customer
    ) customersv0_ where customersv0_.id=7
Hibernate: select customerda0_.id as id4_, customerda0_.name as name4_, customerda0_.type as type4_, customerda0_.age as age4_ from customer customerda0_ where customerda0_.id=7
Hibernate: update customer set name=?, type=?, age=? where id=?

setting it to false
Code:
Hibernate: select customersv0_.id as id5_, customersv0_.name as name5_, customersv0_.type as type5_, customersv0_.age as age5_ from (
SELECT customer.id, customer.name, customer.age, CASE WHEN customer.type = 'P'  THEN 'Partner'  WHEN customer.type = 'I'  THEN 'Individual'  WHEN customer.type = 'C'  THEN 'Company'  ELSE 'Other'  END AS type FROM customer
    ) customersv0_ where customersv0_.id=7
Hibernate: select customerda0_.id as id4_, customerda0_.name as name4_, customerda0_.type as type4_, customerda0_.age as age4_ from customer customerda0_ where customerda0_.id=7
Hibernate: update customer set name=?, type=?, age=? where id=?
Hibernate: select customersv0_.id as id5_, customersv0_.name as name5_, customersv0_.type as type5_, customersv0_.age as age5_ from (
SELECT customer.id, customer.name, customer.age, CASE WHEN customer.type = 'P'  THEN 'Partner'  WHEN customer.type = 'I'  THEN 'Individual'  WHEN customer.type = 'C'  THEN 'Company'  ELSE 'Other'  END AS type FROM customer
    ) customersv0_ where customersv0_.id=7

So we are getting the data from ehcache, not from the database, if we set log4j priority to "DEBUG", here is what we will get
Code:
...
[DEBUG] 2008-04-22 12:53:35,408 ReadWriteCache:246 - Updating: firstTest.CustomerData#7
[DEBUG] 2008-04-22 12:53:35,408 EhCache:68 - key: firstTest.CustomerData#7
[DEBUG] 2008-04-22 12:53:35,408 MemoryStore:135 - calms.hibernate.firstTest.CustomerDataCache: calms.hibernate.firstTest.CustomerDataMemoryStore hit for firstTest.CustomerData#7
[DEBUG] 2008-04-22 12:53:35,408 ReadWriteCache:263 - Updated: firstTest.CustomerData#7
[DEBUG] 2008-04-22 12:53:35,408 UpdateTimestampsCache:65 - Invalidating space [customer], timestamp: 4951585650311169
[DEBUG] 2008-04-22 12:53:35,408 QueryPlanCache:76 - located HQL query plan in cache (from CustomersView where id=7)
[DEBUG] 2008-04-22 12:53:35,408 HQLQueryPlan:150 - find: from CustomersView where id=7
[DEBUG] 2008-04-22 12:53:35,408 QueryParameters:277 - named parameters: {}
[DEBUG] 2008-04-22 12:53:35,408 StandardQueryCache:102 - checking cached query results in region: calms.hibernate.org.hibernate.cache.StandardQueryCache
[DEBUG] 2008-04-22 12:53:35,408 EhCache:68 - key: sql: select customersv0_.id as id5_, customersv0_.name as name5_, customersv0_.type as type5_, customersv0_.age as age5_ from (
SELECT customer.id, customer.name, customer.age, CASE WHEN customer.type = 'P'  THEN 'Partner'  WHEN customer.type = 'I'  THEN 'Individual'  WHEN customer.type = 'C'  THEN 'Company'  ELSE 'Other'  END AS type FROM customer
    ) customersv0_ where customersv0_.id=7; parameters: ; named parameters: {}
[DEBUG] 2008-04-22 12:53:35,408 MemoryStore:135 - calms.hibernate.org.hibernate.cache.StandardQueryCacheCache: calms.hibernate.org.hibernate.cache.StandardQueryCacheMemoryStore hit for sql: select customersv0_.id as id5_, customersv0_.name as name5_, customersv0_.type as type5_, customersv0_.age as age5_ from (
SELECT customer.id, customer.name, customer.age, CASE WHEN customer.type = 'P'  THEN 'Partner'  WHEN customer.type = 'I'  THEN 'Individual'  WHEN customer.type = 'C'  THEN 'Company'  ELSE 'Other'  END AS type FROM customer
    ) customersv0_ where customersv0_.id=7; parameters: ; named parameters: {}
[DEBUG] 2008-04-22 12:53:35,408 StandardQueryCache:156 - Checking query spaces for up-to-dateness: [Customer, (
SELECT customer.id, customer.name, customer.age, CASE WHEN customer.type = 'P'  THEN 'Partner'  WHEN customer.type = 'I'  THEN 'Individual'  WHEN customer.type = 'C'  THEN 'Company'  ELSE 'Other'  END AS type FROM customer
    )]
[DEBUG] 2008-04-22 12:53:35,408 EhCache:68 - key: Customer
[DEBUG] 2008-04-22 12:53:35,408 MemoryStore:138 - calms.hibernate.org.hibernate.cache.UpdateTimestampsCacheCache: calms.hibernate.org.hibernate.cache.UpdateTimestampsCacheMemoryStore miss for Customer
[DEBUG] 2008-04-22 12:53:35,408 Cache:661 - calms.hibernate.org.hibernate.cache.UpdateTimestampsCache cache - Miss
[DEBUG] 2008-04-22 12:53:35,408 EhCache:77 - Element for Customer is null
[DEBUG] 2008-04-22 12:53:35,408 EhCache:68 - key: (
SELECT customer.id, customer.name, customer.age, CASE WHEN customer.type = 'P'  THEN 'Partner'  WHEN customer.type = 'I'  THEN 'Individual'  WHEN customer.type = 'C'  THEN 'Company'  ELSE 'Other'  END AS type FROM customer
    )
[DEBUG] 2008-04-22 12:53:35,408 MemoryStore:138 - calms.hibernate.org.hibernate.cache.UpdateTimestampsCacheCache: calms.hibernate.org.hibernate.cache.UpdateTimestampsCacheMemoryStore miss for (
SELECT customer.id, customer.name, customer.age, CASE WHEN customer.type = 'P'  THEN 'Partner'  WHEN customer.type = 'I'  THEN 'Individual'  WHEN customer.type = 'C'  THEN 'Company'  ELSE 'Other'  END AS type FROM customer
    )
[DEBUG] 2008-04-22 12:53:35,408 Cache:661 - calms.hibernate.org.hibernate.cache.UpdateTimestampsCache cache - Miss
[DEBUG] 2008-04-22 12:53:35,408 EhCache:77 - Element for (
SELECT customer.id, customer.name, customer.age, CASE WHEN customer.type = 'P'  THEN 'Partner'  WHEN customer.type = 'I'  THEN 'Individual'  WHEN customer.type = 'C'  THEN 'Company'  ELSE 'Other'  END AS type FROM customer
    ) is null
[DEBUG] 2008-04-22 12:53:35,408 StandardQueryCache:117 - returning cached query results
[DEBUG] 2008-04-22 12:53:35,408 DefaultLoadEventListener:171 - loading entity: [firstTest.CustomersView#7]
[DEBUG] 2008-04-22 12:53:35,408 DefaultLoadEventListener:332 - attempting to resolve: [firstTest.CustomersView#7]
[DEBUG] 2008-04-22 12:53:35,423 DefaultLoadEventListener:349 - resolved object in session cache: [firstTest.CustomersView#7]
[DEBUG] 2008-04-22 12:53:35,423 JDBCContext:237 - after autocommit
[DEBUG] 2008-04-22 12:53:35,423 ConnectionManager:404 - aggressively releasing JDBC connection
[DEBUG] 2008-04-22 12:53:35,423 SessionImpl:422 - after transaction completion
[DEBUG] 2008-04-22 12:53:35,423 SessionImpl:273 - closing session
[DEBUG] 2008-04-22 12:53:35,423 ConnectionManager:375 - connection already null in cleanup : no action

so we are Invalidating space [customer], yet still we get a memory hit!! am I missing something here?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2008 7:58 am 
Newbie

Joined: Tue Nov 18, 2008 7:48 am
Posts: 1
Do you allready found an solution?

I came across the same problem and found the following solution:

You mapped your "Customer" to the "customer" table:

Code:
<class
      name="Customer"
      table="customer"
   >


and in your "CustomersView" you want to synchonize with the "Customer" table:

Code:
<synchronize table="Customer"/>


I had the same Problem!
Your problem should be solved if you synchronize to "customer" instead of "Customer":

Code:
<synchronize table="customer"/>


The name of the table ist case sensitive... that's all


Top
 Profile  
 
 Post subject: Re: SubSelect stale data
PostPosted: Wed Jan 20, 2010 9:49 am 
Newbie

Joined: Tue Feb 03, 2009 2:30 pm
Posts: 3
There is an additional gotcha for those using Microsoft SQL Server. Not only is case-sensitivity an issue, you may also need to prepend the schema to the table name in the synchronize element
Code:
<synchronize table="dbo.Item" />


Huge thanks to Dirk for setting me on the right track and to the original poster. I think the docs in Hibernate could be clearer on this.


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.