【问题标题】:Spring JPARepository not Lazy still EagerSpring JPARepository 不是 Lazy 还是 Eager
【发布时间】:2016-11-16 04:22:16
【问题描述】:

我有 Object Master Location 和 MasterCountries

MasterLocation.java

@Entity
@Table(name = "master_location", schema = "public", catalog = "master_db")
@NamedEntityGraphs({
        @NamedEntityGraph(name = "MasterLocation.joinsWithMasterCountries",attributeNodes = {
                @NamedAttributeNode("masterCountriesByCountryCode")
        }),
        @NamedEntityGraph(name = "MasterLocation.noJoins",attributeNodes = {})
})
public class MasterLocation {
    private String locationCode;
    private String locationName;
    private String countryCode;
    private Integer agencyCode;
    private String port;
    private String place;
    private String userId;
    private Timestamp dateCreated;
    private Timestamp lastModified;
    private String portRefFrom;
    private String portRefTo;
    private String valid;
    private String regionCode;
    private String bookingRef;

    private MasterCountries masterCountriesByCountryCode;
    @ManyToOne(fetch = FetchType.LAZY)
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(referencedColumnName = "iso_3166_1_alpha_2_code", name = "country_Code")
    public MasterCountries getMasterCountriesByCountryCode() {
        return masterCountriesByCountryCode;
    }
}

MasterLocationRepository.java

@Repository
public interface MasterLocationRepository extends JpaRepository<MasterLocation, Integer> {

    @EntityGraph(value = "MasterLocation.noJoins",type = EntityGraph.EntityGraphType.LOAD)
    Page<MasterLocation> findByLocationNameLikeIgnoreCase(String locationName, Pageable pageable);
}

为什么对象 MasterCountries 仍会在 findByLocationNameLikeIgnoreCase 上加载?如何在此存储库中禁用 Fetch MasterCountries?

谢谢

【问题讨论】:

  • 那是你的真实实体吗? @Id 在哪里?还应该有更多的吸气剂...请添加完整的实体而不是 sn-p。

标签: spring-boot spring-data-jpa


【解决方案1】:

我想您正在使用 Hibernate 作为您的 JPA 提供程序。默认情况下,所有单个属性都会立即加载,即使是一对一或多对一关系也被注释为 Lazy。

为了能够延迟加载单个属性,您必须使用 hibernate 的 字节码增强 支持。在this Vlad Mihalcea's post 中,您将了解如何使用 Maven 激活它。您只需将以下插件添加到您的 pom.xml 文件中:

<plugin>
    <groupId>org.hibernate.orm.tooling</groupId>
    <artifactId>hibernate-enhance-maven-plugin</artifactId>
    <version>${hibernate.version}</version>
    <executions>
        <execution>
            <configuration>
                <enableLazyInitialization>true</enableLazyInitialization>
            </configuration>
            <goals>
                <goal>enhance</goal>
            </goals>
        </execution>
    </executions>
</plugin>

还可以尝试将@EntityGraph 注释类型参数更改为 FETCH,而不是 LOAD,或者使用 FetchType.LAZY 注释 masterCountriesByCountryCode。使用您当前的配置 (EntityGraphType.LOAD),未包含在 EntityGraph 定义中的属性将保持其默认的 FETCH/LOAD 配置。更多详情请关注here

another post Vlad 再次谈到字节码增强并提供另一个选项:子实体。我自己没有尝试过子实体选项,所以我无法告诉你它的效果如何。

最后,另一个需要更多工作的选择,但我认为是最好的选择,是使用投影 DTO 仅选择每个案例所需的数据。

【讨论】:

    猜你喜欢
    • 2019-01-21
    • 2019-11-03
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 2011-05-21
    • 2016-07-16
    • 1970-01-01
    相关资源
    最近更新 更多