-->
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.  [ 5 posts ] 
Author Message
 Post subject: changes made to the database are not updated in my session
PostPosted: Tue Apr 15, 2008 9:43 am 
Newbie

Joined: Tue Apr 15, 2008 9:18 am
Posts: 3
Hi,

I have two tables in my database. USER(username, roleID, passwd) and ROLE(roleID, name). Role contains only two entries:
Quote:
1,Admin
2,Customer


the following code
Code:
session.createQuery("from User").list();

Provides me a list of User objects.
Quote:
usera,Admin,securepasswd
userb,Customer,moresecurepasswd


When changes made directly to the underlying database, e.g. to change a user role:
Code:
update user set roleid=1 where username like 'userb';

nothing happens. If i launch a new query on my running session, the result is the same as before.
On the other hand, if I make a new user. session.createQuery delivers me this new user immediately.

here are the two mapping documents:

User.hbm.xml:
Code:
<hibernate-mapping>
    <class name="namespace.User" table="user" schema="public">
        <id name="username" type="string">
            <column name="username" length="20" />
            <generator class="assigned" />
        </id>
        <many-to-one name="role" class="namespace.Role" fetch="select" >
            <column name="roleid" />
        </many-to-one>
        <property name="passwd" type="string">
            <column name="passwd" length="64"/>
        </property>
    </class>
</hibernate-mapping>

Role.hbm.xml
Code:
<hibernate-mapping>
    <class name="namespace.Role" table="role" schema="public">
        <id name="roleid" type="int">
            <column name="roleid" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="name" length="30" />
        </property>
    </class>
</hibernate-mapping>


I guess it has something to do with the mapping between these two tables, but I didn't get into it.

Any help would be really appreciated.

Thanks!
Christian


Top
 Profile  
 
 Post subject: Re: changes made to the database are not updated in my sessi
PostPosted: Tue Apr 15, 2008 10:56 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
It might be reasonably ok if the two queries are happening in the scope of the same session. A hibernate session has a cache that keeps all retrieved entities. See if session.clear() helps with this and if so the stale data is being retrieved from session cache.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 16, 2008 3:55 am 
Newbie

Joined: Tue Apr 15, 2008 9:18 am
Posts: 3
Hello Farzad,

session.clear() doesn't work for me, because the session contains not only the user and role instances from database. session.clear() evicts all loaded instances and that leads to a crash in my application :(

I managed to get the correct values from database by calling a refresh on every object
Code:
ArrayList<User> list = (ArrayList<User>)session.createQuery("from User").list();
for(User u: list){
   session.refresh(u);
}
return list;

But this is a really dirty solution with a lot of unneccesary queries and db traffic.

I'm sure that I missed something in my *.hmb.xml files.

Is there a way to force hibernate to take the data right from the table and not from the cache?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 16, 2008 10:40 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I am surprised you keep a session object for that long. Why is that?


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 16, 2008 10:56 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
Code:

        <many-to-one name="role" class="namespace.Role" fetch="select" update="true">
            <column name="roleid" />
        </many-to-one>
       


you can add attribute update=true in your cfg.xml, so if you do any update, it will requery automatically


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.