简体   繁体   English

Hibernate在同一事务中新创建的记录上的save()之后调用get()

[英]Hibernate calling get() after save() on the 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 running into an issue with my implementation of the open-session-in-view pattern. 我正在使用没有spring的hibernate 3.2.7(不要问,客户端不支持spring),而且我遇到了open-session-in-view模式的实现问题。 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 . 当我通过调用save()将对象持久化到数据库时,然后在该对象上调用get()以加载由数据库中的外键表示的子对象的详细信息。 The problem is that when I call get() none of the child objects are being loaded. 问题是,当我调用get()时,没有任何子对象被加载。 If I call the get() method from a new transaction everything loads as expected. 如果我从新事务中调用get()方法,则所有内容都按预期加载。

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();

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: 持久化对象后,请求将传递给struts操作类,该类调用服务中的get()方法,该方法具有此代码以加载合同:

Session session = HibernateUtil.getSession();

session.setFlushMode(FlushMode.MANUAL);

try {

    contract = contract.get();

    ...
}

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. 加载持久合同时调用相同的get动作方法,该方法工作正常,因此我知道get()方法在自己的事务中被隔离时可以正常工作。 The only time it doesn't work is when its called right after the save() from within the same transaction. 它不起作用的唯一时间是在同一事务中从save()之后调用它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 休眠保存并使用相同的会话,相同的事务 - hibernate save and get using same session ,same transaction 交易后休眠自动保存 - Hibernate automatic save after Transaction 在上一个事务失败后,hibernate / spring尝试插入而不是保存(db中的记录已存在) - hibernate/spring is trying to insert rather than save (record already exists in db) after the previous transaction failed 使用相同的对象多次调用休眠的 save() 方法会在数据库中插入新记录吗? - Does calling multiple times save() method of hibernate with same object insert new record in DB? GAE Objectify在同一事务中的事务中创建的加载对象 - GAE Objectify load object created in transaction within same transaction Hibernate没有返回新插入的记录 - Hibernate not returning newly inserted record Hibernate save()和事务回滚 - Hibernate save() and transaction rollback 休眠如何在 session.save(object) 上获取新插入的记录主键 - How is hibernate getting the newly inserted record primary key on session.save(object) 休眠:记录到数据库后无法获取数据 - Hibernate: cant get data after record to DB Java Hibernate save an object and update a different object using the same session in the same transaction only update and not save - Java Hibernate save an object and update a different object using the same session in the same transaction only update and not save
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM