本文介绍了EntityNotFoundException:在OneToOne映射上找不到ID为5的子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初,我开始在@OneToOne映射方案中解决N + 1选择问题,最后我将多个选择查询减少为一个.我尝试使用EntityGraphs解决N + 1问题,但是它不起作用.所以我通过将fetch Type设置为Lazy并设置Optional = false来将@OneToOne映射更改为LAZY.我正在使用Hibernate JPA.

I initially started of solving a N+1 select issue in my @OneToOne mapping scenario, i finally was able to reduce the multiple select queries to one. I tried using EntityGraphs to resolve the N+1 issue, but it does not work. So i changed my @OneToOne mapping to LAZY, by setting the fetch Type to Lazy and setting Optional=false.I am using Hibernate JPA.

我进行了如下所示的更改

I made changes as shown below

Public Class Parent {

  @OneToOne(
      optional = false,
      mappedBy = "parent",
      fetch = FetchType.LAZY,
      cascade = {CascadeType.ALL})
  @NotFound
  private Child child;
}

在我的孩子"课程中,

Public Class Child {

  @Id
  private String Id;

  @OneToOne
  @PrimaryKeyJoinColumn
  private Parent parent;

}

Repository类如下,

The Repository class is as below ,

public interface ParentChildRepository extends PagingAndSortingRepository<Transaction, String> {


  @EntityGraph(value = "Parent.EntityGraph", type = EntityGraphType.LOAD)
  default List<Transaction> findall() {
    return StreamSupport.stream(findAll().spliterator(), false).collect(Collectors.toList());
  }
}

没有optional = false的情况下,LazyLoading不起作用,并且我得到N + 1个Select查询语句,但是当我设置了optional = false时,抛出EntityNotFoundException异常.当optional = false语句被删除/设置为true时,不会引发EntityNotFoundException,但是它不能解决我的N + 1选择问题.

Without the optional = false, LazyLoading does not work and i get N+1 Select query statements, but when i do set optional = false,EntityNotFoundException exception is thrown. The EntityNotFoundException is not thrown when the optional=false statement is removed/set to true, but it doesn't fix my N+1 select issue.

请提供一些建议.

推荐答案

尝试使用未找到的忽略项,如下所示.

Try to use not found ignore like below.

@NotFound(action = NotFoundAction.IGNORE)

请参考

这篇关于EntityNotFoundException:在OneToOne映射上找不到ID为5的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 14:10