无法使FetchType.EAGER与@ElementCollection一起使用



我在一个变量上有以下注释:

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "question_translation",
foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_question_translation_question"),
joinColumns = @JoinColumn(name = "question_id"))
@MapKeyColumn(name = "language", nullable = false)
@MapKeyEnumerated(EnumType.STRING)
@Column(name = "translation", nullable = false)
private Map<Language, String> translations;

当我在@Transactional之外执行translations.get(Language.EN)时,我得到:org.hibernate.LazyInitializationException: could not initialize proxy - no Session

版本:

spring boot: <version>2.0.3.RELEASE</version>
<hibernate.version>5.2.17.Final</hibernate.version>
<hibernate-jpa-2.1-api.version>1.0.2.Final</hibernate-jpa-2.1-api.version>

我错过了什么?

编辑:

经过进一步搜索,我认为问题是因为我使用了spring数据和它们的存储库。。。他们似乎忽略了提取类型。。。

我知道错在哪里:spring数据会忽略fetch类型,直到您在存储库和实体中的方法上都指定了@EntityGraph注释。

问题是,不能重写JpaRepository中的方法并将@EntityGraph添加到其中,因为它被忽略了。

您需要在存储库中编写自己的方法。。。例如:

@EntityGraph(value = "graphNameDefinedOnEntity", type = EntityGraph.EntityGraphType.LOAD)
QuestionEntity getById(Long id);

这与CCD_ 5提供的CCD_。非常令人困惑。因此,最好将其命名为getByIdEagerlyLoaded,并使用@Query注释自己编写查询

相关内容

  • 没有找到相关文章

最新更新