-->
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: how to prevent getting stale data from session
PostPosted: Thu Feb 03, 2005 6:04 pm 
Regular
Regular

Joined: Fri Jan 28, 2005 3:11 am
Posts: 81
When I load in an enity called group from a session as shown in the code below (which also apperently loads in the GroupUsers associated with that group as can be seen from the SQL below, also see the hibernate mapping), then perform some updates and then call this code again for the second time to see my changes, I am not getting the groupUsers the way they now appear in the database, but rather a cached version from the last time the code was called. As soon as I do a page refresh, I get the right data. Now, the SQL is being regenerated, I checked. So it would appear that it's the outcome of the query that's being cached, but I am not an expert.

Hibernate version:
2.1.8

Mapping documents:

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<!-- This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<hibernate-configuration>

<session-factory>
<!-- properties -->
<property name="connection.username">root</property>
<property name="connection.url">jdbc:mysql://localhost:3306/workflow</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="connection.password">blah</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<!-- mapping files -->
<mapping resource="com/talisen/workflow/hibernate/Group.hbm.xml"/>
<mapping resource="com/talisen/workflow/hibernate/User.hbm.xml"/>
<mapping resource="com/talisen/workflow/hibernate/GroupUser.hbm.xml"/>
</session-factory>

</hibernate-configuration>

Group.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<!--
Based on the Mapping file for the Category class of CaveatEmptor.
-->
<hibernate-mapping package="com.talisen.workflow.hibernate">

<class name="Group" table="GROUPYO">
<!-- Common id property. -->
<id name="groupId" type="java.lang.Integer" column="GROUP_ID"
unsaved-value="null">
<generator class="native" />
</id>
<property name="groupName" type="java.lang.String">
<column name="GROUP_NAME" not-null="true"
unique-key="UNIQUE_NAME_AT_LEVEL" />
</property>
<property name="groupDesc" column="GROUP_DESC"
type="java.lang.String" not-null="false" />

<set name="groupUsers" inverse="true" cascade="none"
order-by="STATUS_ID">
<key column="GROUP_ID" />
<one-to-many
class="com.talisen.workflow.hibernate.GroupUser" />
</set>
</class>

</hibernate-mapping>

User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<!--
Based on the Mapping file for the Item class of CaveatEmptor.
-->
<hibernate-mapping package="com.talisen.workflow.hibernate">

<class name="User" table="USER">

<!-- Common id property. -->
<id name="userId" type="java.lang.Integer" column="USER_ID"
unsaved-value="null">
<generator class="native" />
</id>

<!-- Name of the item is immutable. -->
<property name="userName" type="java.lang.String"
column="USER_NAME" not-null="true" />

<set name="groupUsers" inverse="true" cascade="none"
order-by="STATUS_ID">
<key column="USER_ID" />
<one-to-many
class="com.talisen.workflow.hibernate.GroupUser" />
</set>
</class>
</hibernate-mapping>

GroupUser.hbm.xml


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="com.talisen.workflow.hibernate">
<!--Based on the mapping file for the CategorizedItem class of CaveatEmptor. -->

<class name="GroupUser" table="USER_GROUP">

<composite-id name="groupUserKey" class="GroupUser$Id"
unsaved-value="any">
<key-property name="groupId" access="field"
column="GROUP_ID" />

<key-property name="userId" access="field"
column="USER_ID" />
</composite-id>
<many-to-one name="group" insert="false" update="false"
not-null="true" column="GROUP_ID" />

<many-to-one name="user" insert="false" update="false"
not-null="true" column="USER_ID" />
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
session = SessionFactory.currentSession();
Transaction tx = session.beginTransaction();
group = (Group) session.load(Group.class, groupId);
tx.commit();

Name and version of the database you are using:
MySQL 4.1.8-nt

The generated SQL (show_sql=true):
Hibernate: select group0_.GROUP_ID as GROUP_ID0_, group0_.GROUP_NAME as GROUP_NAME0_, group0_.GROUP_DESC as GROUP_DESC0_ from GROUPYO group0_ where group0_.GROUP_ID=?

Hibernate: select groupusers0_.GROUP_ID as GROUP_ID__, groupusers0_.USER_ID as USER_ID__, groupusers0_.GROUP_ID as GROUP_ID1_, groupusers0_.USER_ID as USER_ID1_, user1_.USER_ID as USER_ID0_, user1_.USER_NAME as USER_NAME0_ from USER_GROUP groupusers0_ left outer join USER user1_ on groupusers0_.USER_ID=user1_.USER_ID where groupusers0_.GROUP_ID=? order by groupusers0_.STATUS_ID

Hibernate: select groupusers0_.USER_ID as USER_ID__, groupusers0_.GROUP_ID as GROUP_ID__, groupusers0_.GROUP_ID as GROUP_ID1_, groupusers0_.USER_ID as USER_ID1_, group1_.GROUP_ID as GROUP_ID0_, group1_.GROUP_NAME as GROUP_NAME0_, group1_.GROUP_DESC as GROUP_DESC0_ from USER_GROUP groupusers0_ left outer join GROUPYO group1_ on groupusers0_.GROUP_ID=group1_.GROUP_ID where groupusers0_.USER_ID=? order by groupusers0_.STATUS_ID


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.