“org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行”但它确实存在

时间:2013-04-06 21:02:22

标签: mysql spring hibernate

我正在使用hibernate作为我的网络服务。

我能够列出所有记录,但无法获得一个记录。

该表包含:

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

我已经读过这可能是因为数据库与我的实体不一致(有些nullable = true,当它不应该),但我已经检查过它并且没有这样的东西。

有人可以帮我一把吗?

提前致谢!

1 个答案:

答案 0 :(得分:6)

您的错误是指名为' Device'的实体。但是您的代码会显示一个实体'设置'它们是一样的吗?

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

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

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

相关问题