Hibernate:org.hibernate.AssertionFailure:com.xxx.Bean 条目中的 null id

发布于 2024-10-18 21:39:14 字数 4918 浏览 7 评论 0原文

我遇到以下错误:

com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract void com.xxx.Service.save(com.xxx.Bean)' threw an unexpected exception: org.hibernate.AssertionFailure: null id in com.xxx.Bean entry (don't flush the Session after an exception occurs)
    at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:385)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:588)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:541)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:383)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)
Caused by: org.hibernate.AssertionFailure: null id in com.xxx.Bean entry (don't flush the Session after an exception occurs)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:82)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:190)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:147)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
    at com.xxx.ServiceImpl.save(ServiceImpl.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    ... 23 more

这是 MySQL 表定义:

CREATE TABLE `tblARF` (
  `ARF_ID` bigint(19) NOT NULL AUTO_INCREMENT,
  `ARF_MANUFACTURER_MNF_ID` bigint(19) NOT NULL,
  ...
  PRIMARY KEY (`ARF_ID`),
  KEY `ARF_MANUFACTURER_MNF_ID` (`ARF_MANUFACTURER_MNF_ID`)
)

这里是 Hibernate 映射:

<class name="Bean" table="tblARF">
  <id name="key" type="long" unsaved-value="null">
    <column name="ARF_ID" not-null="true"/>
    <generator class="identity" />
  </id>
<many-to-one column="ARF_MANUFACTURER_MNF_ID" name="manufacturer"
    class="ManufacturerBean" not-null="true" lazy="false"/>
...
</class>

Java 代码:

Session s = hibernate.getCurrentSession();
Transaction t = s.beginTransaction();
s.merge(bean.getManufacturer());
s.save(bean);
t.commit();

我注意到,如果我删除 KEY ARF_MANUFACTURER_MNF_ID (ARF_MANUFACTURER_MNF_ID) 而不是 AssertionFailure不会发生。有没有办法在不删除 KEY 的情况下避免此错误?

不幸的是,这个解决方案对我不起作用。

I was faced to the following error:

com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract void com.xxx.Service.save(com.xxx.Bean)' threw an unexpected exception: org.hibernate.AssertionFailure: null id in com.xxx.Bean entry (don't flush the Session after an exception occurs)
    at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:385)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:588)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:541)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:383)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)
Caused by: org.hibernate.AssertionFailure: null id in com.xxx.Bean entry (don't flush the Session after an exception occurs)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:82)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:190)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:147)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
    at com.xxx.ServiceImpl.save(ServiceImpl.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    ... 23 more

Here is MySQL table definition:

CREATE TABLE `tblARF` (
  `ARF_ID` bigint(19) NOT NULL AUTO_INCREMENT,
  `ARF_MANUFACTURER_MNF_ID` bigint(19) NOT NULL,
  ...
  PRIMARY KEY (`ARF_ID`),
  KEY `ARF_MANUFACTURER_MNF_ID` (`ARF_MANUFACTURER_MNF_ID`)
)

And here is Hibernate mapping:

<class name="Bean" table="tblARF">
  <id name="key" type="long" unsaved-value="null">
    <column name="ARF_ID" not-null="true"/>
    <generator class="identity" />
  </id>
<many-to-one column="ARF_MANUFACTURER_MNF_ID" name="manufacturer"
    class="ManufacturerBean" not-null="true" lazy="false"/>
...
</class>

Java code:

Session s = hibernate.getCurrentSession();
Transaction t = s.beginTransaction();
s.merge(bean.getManufacturer());
s.save(bean);
t.commit();

I noticed that if I remove KEY ARF_MANUFACTURER_MNF_ID (ARF_MANUFACTURER_MNF_ID) than AssertionFailure doesn't happen. Is there a way to avoid this error without removing the KEY?

This solution doesn't work for me unfortunately.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

洒一地阳光 2024-10-25 21:39:14

答案在这里找到:
https://forum.hibernate.org/viewtopic.php?f=1& ;t=1009141

'order'是SQL中的保留关键字
所以如果你想使用 if 作为一列
name 需要加引号。看
http://docs.jboss .org/hibernate/core/3.6/reference/en-US/html/mapping.html#mapping-quotedidentifiers 了解如何
启用此功能。

The answer was found here:
https://forum.hibernate.org/viewtopic.php?f=1&t=1009141

'order' is a reserved keyword in SQL
so if you want to use if as a column
name it needs to be quoted. See
http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/mapping.html#mapping-quotedidentifiers for instructions how to
enabled this.

岁月苍老的讽刺 2024-10-25 21:39:14

您发布的异常说明了问题是什么:

Caused by: org.hibernate.AssertionFailure: null id in com.xxx.Beanentry (don'tlush the Session after an exception indicates)

在 Hibernate 中发生与会话相关的异常后,您必须丢弃 Hibernate 会话。

但我想这不是你的问题。

你需要设置 Bean 的 ID 或使用 (@GenerateValue(strategy=GenerationType.AUTO)) 代替。

已添加
尝试将:合并替换为保存

The Exception you have posted said what the problem is:

Caused by: org.hibernate.AssertionFailure: null id in com.xxx.Bean entry (don't flush the Session after an exception occurs)

In Hibernate you have to throw away the Hibernate Session after a Session related Exception occures.

But I guess that is not your question.

You need to set the ID of your Bean or use <generator class="native" /> (@GeneratedValue(strategy=GenerationType.AUTO)) instead.

Added
try to replace: merge by save

缺⑴份安定 2024-10-25 21:39:14

我不确定这是否与您的问题有关,但请注意 merge() 的错误用法。

merge() 不会使传递给它的对象持久化,它会返回具有相同状态的另一个实例,因此您应该编写

bean.setManufacturer(s.merge(bean.getManufacturer())); 

I'm not sure whether it's related to your problem, but note the wrong usage of merge().

merge() doesn't make the object passed into it persistent, it returns another instance with the same state instead, so you should write

bean.setManufacturer(s.merge(bean.getManufacturer())); 
萌吟 2024-10-25 21:39:14

有时真正的错误在于其他地方。要检查确切的错误,请使用调试模式并使用异常的 printstacktrace() 方法。它给你更详细的原因。就我而言,我使用了 String,其中列的类型为 int。

Sometimes the real error lies somewhere else. To check the exact error use Debug mode and make use of printstacktrace() method of exception. It gives you the more detailed reason. In my case I had used String where the column was of type int.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文