我在Java 6 上运行的websphere 8.5.5.4上使用Hibernate 4.2.21数据库:Microsoft SQL Server 2012有时,当我尝试使用以下代码更新实体时:this.transaction.begin();// lots of lines of code here before the mergemerge(carentrypermitrequest);this.transaction.commit(); // exception here这就是我获得会话工厂的方式:protected SessionFactory getSessionFactory() { try { return (SessionFactory) new InitialContext().lookup("SessionFactory"); } catch (Exception e) { log.error("Could not locate SessionFactory in JNDI", e); throw new IllegalStateException("Could not locate SessionFactory in JNDI"); } }这就是我获得交易的方式:public final UserTransaction transaction = getUserTransaction();protected UserTransaction getUserTransaction() { try { return (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); } catch (NamingException e) { e.printStackTrace(); System.err.println("Could not locate UserTransaction in JNDI" + e.getMessage()); throw new IllegalStateException("Could not locate UserTransaction in JNDI"); } }这是合并方法:public CarEntryPermitRequest merge(CarEntryPermitRequest detachedInstance) { try { CarEntryPermitRequest result = (CarEntryPermitRequest) sessionFactory.getCurrentSession() .merge(detachedInstance); return result; } catch (RuntimeException re) { throw re; } }
3 回答
慕森王
TA贡献1777条经验 获得超3个赞
过去,我一直在为同样的问题苦苦挣扎:我试图用一些新数据更新一个实体,并不断地接收它RollBackException
而没有任何痕迹。
在调试 Hibernate 的源代码数小时后,我发现之前从 Hibernate 的源代码中抛出了另一个完全不同的异常,并吞入了一个空catch
块并从任何外部视图中消失。这打破了EntityManager
我用来合并我的数据的。
特别是,Hibernate 试图实例化流程中所需的另一个实体,但找不到默认的公共构造函数。
我catch
仍然不知道Hibernate 的代码如何包含包含空块的代码行。我使用的版本是4.3.5.Final
.
我猜您遇到了同样的问题:一些异常,可能与我面临的异常(缺少公共默认构造函数)的性质不同,从休眠层抛出,在那里被捕获,然后无论如何都没有管理。
我建议你操作相同的过程:
下载 Hibernate 的源代码并在整个过程调用的方法中迭代其中,尤其要留意空的 catch 块。 请注意,在我的情况下,空的 catch 块不属于RollBackException
!
吃鸡游戏
TA贡献1829条经验 获得超7个赞
我认为您每次都必须初始化交易,但您在这一行中将交易声明为最终交易:
public final UserTransaction transaction = getUserTransaction();
因此,每次需要开始事务时,更改代码以根据您的连接对象生成新的事务对象。
添加回答
举报
0/150
提交
取消