可以在OneToOne Mapping的关联实体上调用spring data jpa findAll [英] spring data jpa findAll can be called on associated entity of OneToOne Mapping

查看:39
本文介绍了可以在OneToOne Mapping的关联实体上调用spring data jpa findAll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下两个实体类和对应的存储库接口

I have two entity classes as below and corresponding repository interface

实体 1

@Data
@NoArgsConstructor
@Entity
@Table(name = "person_details")
public class PersonDetails {
    @Id
    private String pid;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "exist_flag")
    private String existFlag;

    @OneToOne(mappedBy = "personDetails", cascade = CascadeType.ALL)
    private AddressDetails addressDetails;
}

实体 2

@Data
@NoArgsConstructor
@Entity
@Table(name = "address_details")
public class AddressDetails {
    @Id
    private String pid;

    private String street;

    @Column(name = "address_exist_flag")
    private String addressExistFlag;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "pid", insertable = false, updatable = false)
    private PersonDetails personDetails;

}

对应的repository接口

Corresponding repository interfaces

public interface PersonDetailsRepository extends JpaRepository<PersonDetails, String> {

}

public interface AddressDetailsRepository extends JpaRepository<AddressDetails, String> {

}

如果我在 PersonDetailsRepository 实例上调用 findAll,我应该获得PersonDetails 和 AddressDetails 的详细信息,这是我在当前实现中获得的.

If i call findAll on PersonDetailsRepository instance, I should get details of both PersonDetails and AddressDetails which I am getting with current implementation.

如果我在 AddressDetailsRepository 实例上调用 findAll,我应该获得仅 AddressDetails 的详细信息,而我在当前实现中 获得这些详细信息.如何在不影响 PersonDetailsRepository 实例上的 findAll 的情况下实现这一点?

If i call findAll on AddressDetailsRepository instance, I should get details of only AddressDetails which I am not getting with current implementation. How can I achieve this without impacting findAll on PersonDetailsRepository instance?

推荐答案

默认情况下,hibernate 中仅延迟加载一对多关系.即使我们指示 hibernate 延迟加载,hibernate 也会忽略该提示.为了解决这个问题,我们需要启用字节码增强.

By default only one to many relation is lazily loaded in hibernate. Even if we instruct hibernate to lazily load, hibernate disregards that hint. To overcome this problem, we need to enable bytecode enhancement.

字节码增强

字节码增强插件,增强实体类的字节码,让我们可以利用No-proxy的懒加载策略.我们可以通过以下方式在pom.xml文件中定义插件

Bytecode enhancement plugin that enhances the bytecode of entity classes and allows us to utilize No-proxy lazy fetching strategy.We can define the plugin in pom.xml file in the following way

<build>
<plugins>
    <plugin>
        <groupId>org.hibernate.orm.tooling</groupId
        <artifactId>hibernate-enhance-maven-plugin</artifactId>
        <version>5.4.2.Final</version>
        <executions>
        <execution>
        <configuration>           <enableLazyInitialization>true</enableLazyInitialization>                 </configuration>
        <goals>
            <goal>enhance</goal>
        </goals>
        </execution>
        </executions>
    </plugin>
</plugins>
</build>

启用无代理延迟关联现在我们需要在实体类中添加@LazyToOne注解,让hibernate知道我们要为关联实体启用无代理延迟获取.

Enabling no proxy lazy associations Now we need to add @LazyToOne annotation in entity classes to let hibernate know that we want to enable no proxy lazy fetching for associated entities.

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @LazyToOne(LazyToOneOption.NO_PROXY)
    @JoinColumn(name = "pid", insertable = false, updatable = false)
    private PersonDetails personDetails;

这篇关于可以在OneToOne Mapping的关联实体上调用spring data jpa findAll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆