【问题标题】:JPA 2 - INNER JOIN calls Query againJPA 2 - INNER JOIN 再次调用 Query
【发布时间】:2015-09-23 01:45:55
【问题描述】:

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

我的课程是:

@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_左外连接 tbltranshora trasladoho1_ on traslado0_.TransladosID=trasladoho1_.thr_TransladosID where traslado0_.tra_Status=?按 traslado0_.tra_De、traslado0_.tra_Para 订购 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? 休眠:选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ from tbltranshora trasladoho0_ where trasladoho0_.thr_TransladosID=? Hibernate:从 tbltranshora trasladoho0_ 中选择 trasladoho0_.TranshoraID 作为 Transhor1_13_0_,trasladoho0_.thr_qtdHoras 作为 thr_qtdH2_13_0_,trasladoho0_.thr_TransladosID 作为 thr_Tran3_13_0_ where trasladoho0_.thr_TransladosID=?

【问题讨论】:

    标签: java hibernate jpa jpa-2.0


    【解决方案1】:

    您正面临选择 n+1 的问题

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      相关资源
      最近更新 更多