【发布时间】: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