简体   繁体   English

休眠 OneToONe 延迟获取

[英]Hibernate OneToONe Lazy Fetching

I know this question was asked several times but I couldn't find a clear example and answer about this topic(I also tried other possible solutions).我知道这个问题被问了好几次,但我找不到关于这个主题的明确例子和答案(我也尝试了其他可能的解决方案)。

I am using Spring JPA and Hibernate and trying to make a lazy fetch for a OneToONe relation.我正在使用Spring JPAHibernate并尝试对 OneToONe 关系进行延迟获取。 I have 2 simple Entity classes, one Repository class and using h2 database to lazy load an entity.我有 2 个简单的实体类,一个 Repository 类并使用 h2 数据库来延迟加载实体。 I tried build time bytecode instrumentation to achive this and these are my classes.我尝试构建时间字节码检测来实现这一点,这些是我的课程。

Class A A级

@Entity
public class A {

    private String name;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne(optional = false, fetch = FetchType.LAZY, mappedBy = "a")
    @LazyToOne(LazyToOneOption.NO_PROXY)
    private B b;
}

Class B B级

@Entity
public class B {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "A_ID")
    private A a;
}

Repository存储库

public interface ARepository extends JpaRepository<A, Long> {
    A findByName(String name);
}

pom.xml bytecode enhancer pom.xml字节码增强器

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

and lastly initdata.sql for h2 database最后是 h2 数据库的initdata.sql

insert into a (name, id) values ('a', 1);
insert into b (a_id, id) values (1, 1);

When I call findByName() method in a test class it still executes 2 queries for A and B .当我在测试类中调用findByName()方法时,它仍然对AB执行2个查询。 How can I lazy fetch B class ?我怎样才能懒惰地获取B类? Thanks in advance.提前致谢。

In this tutorial has answer on your qustions教程中对您的问题有答案

That's because Hibernate needs to know if it shall initialize the manuscript attribute with null or a proxy class.那是因为 Hibernate 需要知道它是否应该使用 null 或代理类初始化手稿属性。 It can only find that out, by querying the manuscript table to find a record that references this Book entity.它只能通过查询手稿表来找到引用此 Book 实体的记录。 The Hibernate team decided that if they have to query the manuscript table anyways, it's best to fetch the associated entity eagerly. Hibernate 团队决定,如果他们无论如何都必须查询手稿表,最好急切地获取关联的实体。

As I know it is imposible to avoid 2 queries, because Hibernate needs to know what should be inserted in your "one to one field" - proxy or null.据我所知,避免 2 个查询是不可能的,因为 Hibernate 需要知道应该在“一对一字段”中插入什么 - 代理或空值。 Proxy is inserted to your object if theres is related record in secondary table, NULL if there isn't any records.如果辅助表中有相关​​记录,则将代理插入到您的对象中,如果没有任何记录,则为 NULL。 So hibernate execute second query to make a check.所以休眠执行第二个查询进行检查。 Insted "one to one" you can use "many to one" Insted“一对一”你可以使用“多对一”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM