File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Hibernate with JSF: missing database updates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate with JSF: missing database updates" Watch "Hibernate with JSF: missing database updates" New topic

Hibernate with JSF: missing database updates

Steven Gollery

Joined: Nov 09, 2006
Posts: 18
I'm not sure if I should post this here or in the JSF forum -- the problem is definitely a combination of the two.

I have a JSF page displaying a list of objects that have been retrieved by a hibernate query. If I then navigate to a different page (that is not showing the list), and some other process adds, deletes or edits the database entries for those objects, and afterwards I navigate back to the first page, I see the old values even though the list is rebuilt every time by repeating the query.

Here's the class that queries the database and builds the list:

public class MaintainLists {
private String medCondSelectedItem;
private SessionFactory sf;

public MaintainLists() {

public List<SelectItem> getMedCondList() {
List<SelectItem> medConds = new Vector<SelectItem>();
Session session = sf.openSession();
System.out.println("querying for medical conditions");
Criteria query = session.createCriteria(MedicalCondition.class);
List<MedicalCondition> results = (List<MedicalCondition> query.list();
if (results != null && results.size() > 0) {
for (MedicalCondition result : results) {
medConds.add(new SelectItem(result.getId().toString(), result.getName()));
return medConds;

public void setMedCondList() {

public String getMedCondSelectedItem() {
return medCondSelectedItem;

public void setMedCondSelectedItem(String medCondSelectedItem) {
this.medCondSelectedItem = medCondSelectedItem;

private void makeSessionFactory() {
if (sf == null) {
try {
Configuration cfg = new Configuration();
sf = cfg.buildSessionFactory();
} catch (Throwable ex) {
// Log exception!
throw new ExceptionInInitializerError(ex);


(In the real application, this is several classes, but in the interests of brevity I've mashed them together.)

As you can see, each time the list of MedicalCondtions is displayed, there is a new query in a new session. I would think that would be enough to force Hibernate to get fresh data from the database, but apparently not.

I can see in the Tomcat console window that this method is being called each time the page is displayed, so it doesn't seem to be an issue either with the browser cache or some cache inside Tomcat.

I'm saving JSF state on the server, not on the browser, so I don't think it's a problem of having stale data passed back in the user's HTTP session.

The hibernate mapping file looks like this:

<hibernate-mapping package="jsfTest.model">
<id name="id" column="medcon_key" type="long">
<generator class="native" />
<property name="name" column="name"/>
<property name="description" column="description" type="text"/>

and the MedicalCondition class is similarly simple:

public class MedicalCondition {
private Long id;
private String name;
private String description;
... usual sets and gets ...

I'm using Hibernate 3.2, MyFaces 1.1.4, and Tomcat 5.20.

If anyone has any ideas what I need to do to get the Hibernate query to return current data, it would be very helpful.

Thanks in advance,

Steven Gollery
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Actually the problem is neither Hibernate nor JSF, but about web applications and the dreaded Back Button problem. I click the Back button and the page that displays can be the page that is cached by the browser, and you have no control over it at all.

Now that is where something like JBoss Seam comes along, not only will it save some code for you, like translating your backing beans into your mapped object, etc. But with a Conversation State and Seam State Management, the Back Button is automatically handled for you. (Sorry about the little plug here, but it does match the context of your problem completely)


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Steven Gollery

Joined: Nov 09, 2006
Posts: 18

Sorry, but your explanation doesn't match what I'm seeing.

The getMedCondList method DOES get called every time I display the page. Since this is on the server, I don't think the effect I'm seeing can be blamed on the browser cache -- if the list was being pulled from the cache, would the server get a hit at all?

Further, when I add print statements to output the list of conditions, I see that the list doesn't include the object that I have added to the database after the list was read the first time. Again, all of this takes place on the server, so I don't see how the browser cache can be involved.

Having said that: I have started to look at Seam and it definitely looks interesting. Thanks for the push in that direction.

Steven Gollery
I agree. Here's the link:
subject: Hibernate with JSF: missing database updates
It's not a secret anymore!