Hibernate calls get () after save () for newly created record within the same transaction

I'm using hibernate 3.2.7 without spring (don't ask, client doesn't support spring) and I'm having a problem with my implementation of the open-session-in-view pattern. When I save an object to the database by calling save (), I then call get () on that object to load the details of the child objects that are represented by foreign keys in the database. The problem is that when I call get (), none of the child objects are loaded. If I call the get () method from a new transaction, everything loads as expected.

This is a snippet from my request filter that opens a session and creates a transaction:

HibernateUtil.openSession();

//get a transaction from JTA
transaction = (UserTransaction)new InitialContext().lookup("java:comp/UserTransaction");

transaction.begin();

// Call the next filter (continue request processing)
chain.doFilter(request, response);

// Commit and cleanup
log.finer("Committing the database transaction");
transaction.commit();

      

Here is a snippet from the persist service level method that saves the transaction:

session.setFlushMode(FlushMode.MANUAL);

contract.save();

//save the update to the database
session.flush();

      

After the object is saved, the request is passed to the struts action class, which calls the get () method on the service from which this code loads the contract:

Session session = HibernateUtil.getSession();

session.setFlushMode(FlushMode.MANUAL);

try {

    contract = contract.get();

    ...
}

      

The same get action method is called when the saved contract is loaded, which works fine, so I know the get () method works correctly when isolated in its transaction. The only time it doesn't work is when it gets called right after save () from one transaction.

+1


source to share


1 answer


Have you tried doing Session.refresh () on an object?



+4


source







All Articles