JPA 实体未找到异常。如何在不考虑关系映射的情况下使用 JPA Criteria 查询进行查询

JPA EntityNotFoundException. How to query without considering relationship mapping using JPA Criteria query

我有一个产品实体。一种产品可以是另一种产品的子产品。关系与 OneToMany 映射如下。

@OneToMany(mappedBy = "parentProd")
private Set<Products> childProd = new HashSet<>();

@ManyToOne
@JsonIgnoreProperties("childProd")
private Products parentProd;

由此映射生成的 parent_product_id 列以某种方式包含无效的 parent_ids。因此,当我尝试使用 JPA 条件查询来查询所有产品时,出现以下错误。

javax.persistence.EntityNotFoundException: Unable to find com.test.Products with id 101 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$JpaEntityNotFoundDelegate.handleEntityNotFound(EntityManagerFactoryBuilderImpl.java:162) at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:234)

如何在不考虑 parent_child 关系的情况下向 select 所有产品实体编写标准查询?

@Entity
@Table(name = "products")
public class Products implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "productName")
    private String productName;

    @OneToMany(mappedBy = "parentProd")
    private Set<Products> childProd = new HashSet<>();

    @ManyToOne
    @JsonIgnoreProperties("childProd")
    private Products parentProd;

    //Getters And Setters
}

以这种方式更改 oneToMany 注释:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "parentProd")

这里fetch = FetchType.Lazy的意思是不明确询问就不会去取数据