gpt4 book ai didi

java - JPA 2 - INNER JOIN 再次调用 Query

转载 作者:行者123 更新时间:2023-12-01 11:07:56 25 4
gpt4 key购买 nike

我正在尝试在 JPA 2 中执行一些查询,但它不断调用另一个查询来获取依赖项的信息

我的类(class)是:

@Entity
@Table(name = "tbltranslados")
public class Traslado implements Serializable {

@OneToOne(optional = true, mappedBy = "traslado", fetch = FetchType.LAZY)
private TrasladoHora hora;
}

@Entity
@Table(name = "tbltranshora")
public class TrasladoHora implements Serializable {

@OneToOne(optional = false,fetch=FetchType.LAZY)
@JoinColumn(nullable = false, name = "thr_TransladosID")
private Traslado traslado;
}

我这样调用查询:

@SuppressWarnings("unchecked")
@Override
public List<Traslado> listarAtivos() {
return this.session.createQuery("SELECT t , h FROM Traslado t LEFT JOIN t.hora h WHERE t.status=:status ORDER BY t.origem , t.destino")
.setParameter("status", true)
.getResultList();
}

但我的结果是:

Hibernate: select traslado0_.TransladosID as Translad1_14_0_, trasladoho1_.TranshoraID as Transhor1_13_1_, traslado0_.tra_Para as tra_Para2_14_0_, traslado0_.tra_De as tra_De3_14_0_, traslado0_.tra_Status as tra_Stat4_14_0_, trasladoho1_.thr_qtdHoras as thr_qtdH2_13_1_, trasladoho1_.thr_TransladosID as thr_Tran3_13_1_ from tbltranslados traslado0_ left outer join tbltranshora trasladoho1_ on traslado0_.TransladosID=trasladoho1_.thr_TransladosID where traslado0_.tra_Status=? order by traslado0_.tra_De, traslado0_.tra_Para Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate: select trasladoho0_.TranshoraID as Transhor1_13_0_, trasladoho0_.thr_qtdHoras as thr_qtdH2_13_0_, trasladoho0_.thr_TransladosID as thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=?

最佳答案

您面临选择 n+1 问题

您获得的第一个查询检索您想要的列表,但为了实例化实体 Traslado 的对象,Hibernate 会为每个对象调用一个 select 语句来查明它是否具有 TrasladoHora 关联,因为关系映射到 TrasladoHora 的表中(我的意思是 TrasladoHora.traslado 是此双向关联的所有者端)。

Here is a answer很多人评论提出了建议,但 Hibernate 文档的 Tuning fetch strategies 部分提到了问题和一些解决方法

编辑:I also respond another question与这个问题相关,也许你能在那里找到更清晰的解释。

关于java - JPA 2 - INNER JOIN 再次调用 Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32729714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com