【问题标题】:Spring data JPA - using @EntityGraph causes "Entity graph specified is not applicable to the entity" warningSpring data JPA - 使用@EntityGraph导致“指定的实体图不适用于实体”警告
【发布时间】:2020-12-08 14:46:51
【问题描述】:

我将我的应用程序从 spring boot 2.2.5 升级到 2.3.3,并且我正在使用带有 5.4.20.Final 的 spring data JPA starter。我的实体在编译时得到了增强。

现在,当我使用带有 attributePaths 属性的 @EntityGraph 注释覆盖来自 JpaRepository 的覆盖 findAll 方法时,我收到了以下警告:

2020-08-19 12:13:41.121  WARN 9348 --- [nio-8060-exec-3] [] [] o.h.engine.internal.TwoPhaseLoad         : Entity graph specified is not applicable to the entity [DictionaryLang(id=1601, name=null, lang=null)]. Ignored.
2020-08-19 12:13:41.483  WARN 9348 --- [nio-8060-exec-3] [] [] o.h.engine.internal.TwoPhaseLoad         : Entity graph specified is not applicable to the entity [DictionaryValueLang(id=3051, lang=null, name=null)]. Ignored.

即使此警告 - 图表已正确获取 - 我只能在日志中看到一个 SQL 查询,并且应用程序的行为与更新前一样。

这是我的存储库代码:

public interface DictionaryRepository extends JpaRepository<Dictionary, Long>, QuerydslPredicateExecutor<Dictionary> {

    @EntityGraph(attributePaths = {"langs", "values", "values.langs"})
    @Override
    Page<Dictionary> findAll(Predicate predicate, Pageable pageable);
}

这是我的实体:

@Entity
@Table(name = "DICTIONARIES")
public class Dictionary {

    @Id
    @SequenceGenerator(name = "SEQ_DICTIONARIES", sequenceName = "SEQ_DICTIONARIES")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_DICTIONARIES")
    private Long id;

    @OrderBy("ordinal ASC")
    @OneToMany(mappedBy = "dictionary", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    private List<DictionaryValue> values;

    @OneToMany(mappedBy = "dictionary", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<DictionaryLang> langs;

}


@Entity
@Table(name = "DICTIONARY_LANGS")
public class DictionaryLang extends BaseEntity {

    @Id
    @SequenceGenerator(name = "SEQ_DICTIONARY_LANGS", sequenceName = "SEQ_DICTIONARY_LANGS")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_DICTIONARY_LANGS")
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @LazyToOne(LazyToOneOption.PROXY)
    @JoinColumn(name = "DICTIONARY_ID")
    private Dictionary dictionary;

}

如何解决这个警告?我可以看到这个警告发生在 hibernate 的 TwoPhaseLoad 类的那些行中:

GraphImplementor fetchGraphContext = session.getFetchGraphLoadContext();
if ( fetchGraphContext != null && !fetchGraphContext.appliesTo( entity.getClass() ) ) {
    LOG.warnf( "Entity graph specified is not applicable to the entity [%s]. Ignored.", entity);
    fetchGraphContext = null;
    session.setFetchGraphLoadContext( null );
}

【问题讨论】:

  • 在此期间您能解决问题吗?我目前面临同样的问题,并拼命寻找任何提示。
  • 找到了一个适合我的解决方案:将 type = EntityGraph.EntityGraphType.LOAD 添加到存储库方法中的 EntityGraph 注释。
  • @Seb 谢谢 - 它也适用于我的情况。我想这是新春季数据中的错误。
  • 我将spring-boot-starter-parent 的版本从2.2.0.RELEASE 提高到2.3.2.RELEASE,现在在我没有更改任何代码的情况下发生了这个问题。遵循@Seb 的建议并在注释上定义EntityGraph.EntityGraphType.LOAD 在我的情况下也解决了它。
  • 也有这个问题。我宁愿不更改要加载的实体图类型,因为这不是我需要的,我不希望其他引用默认为它们的正常行为。

标签: java spring-boot hibernate spring-data-jpa entitygraph


【解决方案1】:

这是因为在春季 2.3.3 中将休眠更新到 5.4.20。

如果你将hibernate tp 5.4.18降级这个问题就会消失,升级到5.4.21也没有用。

在我的例子中,休眠不仅仅是添加一条警告消息,它实际上忽略了 @EntityGraph(attributePaths = {...}) 注释并根据映射执行查询生成,在我的例子中 导致 N+1 的工作台问题和数以千计的查询。 另外,EntityGraph.EntityGraphType.LOAD 并不能解决问题,因为在这种情况下,hibernate 将根据映射获取类型加载@EntityGraph 中未提及的所有映射,如果你大了可能会添加很多查询收藏。

查看关于EntityGraphType的详细信息

o 可以使用降级休眠

 implementation ("org.springframework.boot:spring-boot-starter-data-jpa") {
     // to remove fibernate that came with new spring
     exclude group: "org.hibernate", module: "hibernate-core" 
 }
 // to add old hibernate 
 implementation "org.hibernate:hibernate-core:5.4.18.Final"

这里是休眠中的一个相关问题是https://hibernate.atlassian.net/browse/HHH-14124 据说受影响的版本是 4.5.19,修复版本是 4.5.20。 但是,我在 spring 2.3.3(hibernate 4.5.20) 中遇到了错误

更新:这是针对此错误发布的问题。它已经修复: https://hibernate.atlassian.net/browse/HHH-14212

【讨论】:

  • 实际上我所有的关系都被指定为 LAZY 所以使用EntityGraphType.LOAD 没什么大不了的,但感谢您指出这一点。我宁愿降级整个 ​​Spring Data JPA - 我不确定新版本的 Spring Data 不会与旧的 Hibernate 发生冲突。
猜你喜欢
  • 2016-12-03
  • 2019-10-01
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
  • 2015-09-11
  • 1970-01-01
  • 2013-05-09
  • 2019-09-30
相关资源
最近更新 更多