JPA中的其他查询 [英] Additional queries in JPA

查看:385
本文介绍了JPA中的其他查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类 InvitedPerson Flight ,彼此之间有一对一的关系。以下是它们的注释方式。

I have two classes InvitedPerson and Flight with a one to one relationship with each other. Here is how they are annotated.

public class InvitedTech{
    ...
    @OneToOne(mappedBy="invitedTech", cascade = CascadeType.ALL, fetch=FetchType.LAZY)
    public Flight flight;

    @OneToOne(mappedBy="invitedTech", cascade = CascadeType.ALL, fetch=FetchType.LAZY)
    public Hotel hotel;

    ...
}

public class Flight{
    ...
    @OneToOne
    @JoinColumn(name="invitedTechId", nullable=false)
    public InvitedTech invitedTech;
    ...
}

正如您所见 Flight 是该关系的所有者, InvitedTech 是此双向关系的另一方。 InvitedTech还有 OneToOne Hotel 的关系现在,当我写一个简单的查询来获取所有航班时,它会触发三个查询总计。第一个获得结果,但会激发另外两个查询。

As you can see Flight is the owner of the relationship and InvitedTech is the other side of this bidirectional relationship. InvitedTech also has a OneToOne relationship with Hotel Now, when I write a simple query to fetch all flights, it triggers three queries in total. 1st which gets me the results, but fires 2 additional queries.

List<Flight> flg = JPA.em().createQuery("SELECT flg from Flight flg").getResultList();




  1. 获取所有航班的查询(这是我唯一需要的航班)

  2. 使用InvitedTech和Flight之间的联接查询

  3. 使用inviteTech和酒店之间的联接查询

为什么即使我设置了FetchType = Lazy,查询2& 3仍在执行。我没有访问酒店信息。并且当第一个查询返回数据时,Flight不应再次成为查询。

Why are query 2&3 being executed even though I have set FetchType=Lazy. I am not accessing Hotel Information. And Flight should not be queries again as the first query returns the data.

当我从这两个注释中删除 mappedBy 属性时,一些玩游戏后,这两个附加查询没有得到执行(即只执行第一次)。

After some playing around when I remove mappedBy attribute from both the annotations, those 2 addition queries don't get executed(i.e only 1st gets executed).

为什么 mappedBy 属性会导致执行其他查询,即使 FetchType = Lazy 。有没有办法阻止这个?

Why does the mappedBy attribute cause additional queries to be executed even though FetchType=Lazy. Is there a way to stop this?

推荐答案

我认为这是由于Hibernate的特性之一:

I believe this is due to one of Hibernate's idiosyncrasies:

非可选的一对一关系会被急切加载,无论它们是否被映射为Lazy。

non-optional one-to-one relationships are eagerly loaded regardless of whether they are mapped as Lazy.

这背后的原因是因为引擎必须在关联表中查找 - 确定它是否应该将关联设置为代理或null - 然后它也可以加载关联实体。

The reasoning behind this is that as the engine has to look in the association table anyway - to determine whether it should set the association as a proxy or as null - then it may as well load the associated entity anyway.

我自己经历过这个,据我所知,唯一的方法就是使用optional = false标记关系,告诉Hibernate它总能设置代理。

I have experienced this myself and as far as I know the only way round it is to mark the relationship with optional=false which tells Hibernate it can always set a proxy.

如果关系是可选的,那么唯一的其他选项似乎是字节码检测。

If the relationship is optional then the only other option seems to be byte code instrumentation.

参见:

https:// community .jboss.org / wiki / SomeExplanationsOnLazyLoadingone-to-one

让OneToOne-relationship懒惰

这篇关于JPA中的其他查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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