【问题标题】:"org.hibernate.ObjectNotFoundException: No row with the given identifier exists" but it does exists“org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行”但它确实存在
【发布时间】:2013-03-29 04:05:36
【问题描述】:

我正在为我的网络服务使用休眠。

我可以列出所有记录,但不能只列出一条。

表格包含:

ID (VARCHAR)                      VALUE(BIT)
celiac                               1
rate                                 1
suggestions                          0

显示的错误是:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.pfc.restaurante.models.Device#id="xxxxxx"]
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

以及主要代码:

    @JsonAutoDetect
    @Entity
    @Table(name = "SETTINGS")
    public class Settings implements Serializable{
        @Id
        @Column(name="ID")
        private String id;

        @Column(name="VALUE", nullable=false)
        private boolean value;
    (...)
    }
    //////////////////7
   @Controller
   @RequestMapping("/settingsService")
   public class SettingsServiceController {

    @Autowired
    SettingsService settingsService;

        @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public @ResponseBody Settings find(@PathVariable("id") String id){
        return settingsService.find(id);
    }
          (...)
}

我已经阅读过,这可能是因为 DB 与我的实体不一致(一些 nullable = true 时不应该),但我已经检查过了,没有这样的事情。

有人可以帮我吗?

提前致谢!

【问题讨论】:

  • 这是你的Settings 类,例外是指Device 类。向我们展示真实的日志(带有真实 ID,而不是 xxxxx)、设备表和设备类。
  • 对不起,我的错。然而,设备和设置的错误是相同的,并且 xxxx 是一个真实的测试 ID :)。感谢 Antonio,上述解决方案已成功。

标签: mysql spring hibernate


【解决方案1】:

您的错误涉及一个名为“设备”的实体,但您的代码显示了一个实体“设置”。他们是一样的吗?

我只在两种情况下看到过这个错误:

  1. 主实体在 DB 中不存在,使用了 Session.load()。改用 Session.get() 并检查 null。

  2. 关系破裂。考虑一下:EntityA 拥有与 EntityB 的关系。 EntityB 被删除,而 EntityA 中的 FK 保持不变。因此,每当 HB 尝试加载链接 A-B 时,就会发生错误。这可能在运行正常搜索时发生,甚至在保存/刷新 EntityA(HB 也需要刷新链接)时发生。

【讨论】:

  • 确实,他们给了我同样的错误。对不起,我忘记更改错误了。无论如何,谢谢你!第一个解决方案是正确的。我正在使用 session.load()。我刚刚将其更改为 session.get(),现在可以正常工作了。
猜你喜欢
  • 2012-02-17
  • 2015-06-02
  • 2014-01-14
  • 1970-01-01
  • 2011-02-20
  • 1970-01-01
  • 2013-08-28
相关资源
最近更新 更多