Hibernate在同一事务中新创建的记录上的save()之后调用get() [英] Hibernate calling get() after save() on the newly created record within the same transaction

查看:112
本文介绍了Hibernate在同一事务中新创建的记录上的save()之后调用get()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用没有spring的hibernate 3.2.7(不要问,客户端不支持spring),而且我遇到了open-session-in-view模式的实现问题。当我通过调用save()将对象持久化到数据库时,然后在该对象上调用get()以加载由数据库中的外键表示的子对象的详细信息。问题是,当我调用get()时,没有任何子对象被加载。如果我从新事务中调用get()方法,则所有内容都按预期加载。

I'm using hibernate 3.2.7 without spring (don't ask, client doesn't support spring) and I'm running into an issue with my implementation of the open-session-in-view pattern. When I persist 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 being 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 the session and creates the 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 service layer's save method that saves the transaction:

session.setFlushMode(FlushMode.MANUAL);

contract.save();

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

持久化对象后,请求被传递给调用get()方法的struts动作类在具有此代码的服务中加载合同:

After the object is persisted the request gets passed to a struts action class that calls the get() method in the service which has this code to load the contract:

Session session = HibernateUtil.getSession();

session.setFlushMode(FlushMode.MANUAL);

try {

    contract = contract.get();

    ...
}

相同的获取操作方法在加载一个工作正常的持久合约时调用,所以我知道get()方法在它自己的事务中被隔离时可以正常工作。它不起作用的唯一时间是在同一事务中从save()之后调用它。

The same get action method is called when loading a persisted contract which works fine so I know the get() method works properly when isolated in its own transaction. The only time it doesn't work is when its called right after the save() from within the same transaction.

推荐答案

你试过在对象上做一个Session.refresh()吗?

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

这篇关于Hibernate在同一事务中新创建的记录上的save()之后调用get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆