【问题标题】:JPA - EntityNotFoundException, however DB data existsJPA - EntityNotFoundException,但是数据库数据存在
【发布时间】:2017-09-06 10:53:45
【问题描述】:

我有 4 条记录的简单“类别”表:

ID  NAME
1   phones
2   tablets
3   notebooks
4   tvs

领域类:

package com.rest.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="categories")
public class Category {

    @Id 
    @GeneratedValue
    private Long id;

    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

还有以下简单的 JPA 存储库:

    public interface CategoryRepository extends CrudRepository<Category, Long> {

    }

现在,当我尝试时

categoryRepository.findOne(4)

我收到一个 EntityNotFoundException:

2017-04-11 10:19:28.898 错误 16700 --- [nio-8080-exec-1] oaccC[.[.[/].[dispatcherServlet]:Servlet.service() 用于 servlet [dispatcherServlet]在路径 [] 的上下文中抛出异常 [请求处理失败;嵌套异常是 org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.rest.domain.Category with id 4;嵌套异常是 javax.persistence.EntityNotFoundException: Unable to find com.rest.domain.Category with id 4] with root cause

但是,数据存在于数据库中。

RC 可能是以前我删除了与我的 Category 实体有@ManyToOne 关系的父实体,我猜它被标记为从 entityManager 中删除:

productRepository.delete(productId);

我该怎么办?我需要以某种方式冲洗吗?

【问题讨论】:

  • 如果你认为你已经玩过依赖记录,为什么不能手动删除这条记录,然后再试一次
  • 发布Product 实体可能有助于识别问题(级联等)

标签: java spring hibernate spring-data-jpa


【解决方案1】:

确保在您的 xml hibernate 属性配置中设置 hibernate.hbm2ddl.auto=update

<prop key="hibernate.hbm2ddl.auto">update</prop>

【讨论】:

    猜你喜欢
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 2017-05-26
    • 2021-11-25
    • 1970-01-01
    • 2013-08-16
    相关资源
    最近更新 更多