【问题标题】:Query return null in JPA/Hibernate but not in mysql查询在 JPA/Hibernate 中返回 null 但在 mysql 中不返回
【发布时间】:2015-04-14 14:53:02
【问题描述】:

我正在执行一个查询,但它返回 null 但是当我在 sql 行 cmd 中尝试时它返回结果。

这是我的实体:

@Entity
@Table(name = "appel_offre", catalog = "ao")
public class AppelOffre implements java.io.Serializable {

    private Integer idAppelOffre;
    ...
    private Admin userValidation;
    private Admin userSaisie;
    private Admin userControle;

    ....

    public AppelOffre(Integer idAppelOffre,Admin userSaisie,Admin userControle,Admin userValidation) {
        System.out.println("users"); // <-- code not executed
        this.idAppelOffre = idAppelOffre;
        this.userSaisie = userSaisie;
        this.userControle = userControle;
        this.userValidation = userValidation;

    }

我的查询是:

@Query(" select new AppelOffre( ao.idAppelOffre , ao.userSaisie , ao.userControle , ao.userValidation )  from AppelOffre  ao "
 AppelOffre  FindAOwithUsers(@Param("idao") Integer idao);

生成的查询是:

select appeloffre0_.ID_APPEL_OFFRE as col_0_0_, appeloffre0_.USER_SAISIE as col_1_0_, appeloffre0_.USER_CONTROLE as col_2_0_, appeloffre0_.USER_VALIDATION as col_3_0_ 
from ao.appel_offre appeloffre0_ 
inner join ao.admin admin1_ on appeloffre0_.USER_SAISIE=admin1_.ID 
inner join ao.admin admin2_ on appeloffre0_.USER_CONTROLE=admin2_.ID 
inner join ao.admin admin3_ on appeloffre0_.USER_VALIDATION=admin3_.ID 
where appeloffre0_.ID_APPEL_OFFRE=?

我知道问题出在内部连接上,它们必须是左连接,因为在我的数据库中只有USER_SAISIE,而不是null

我用左连接试试:

@Query(" select new AppelOffre( ao.idAppelOffre , ao.userSaisie , ao.userControle , ao.userValidation )  from AppelOffre  ao "
        + " left join ao.userSaisie  "
        + " left join ao.userControle  "
        + " left join ao.userValidation  "
         + " where ao.idAppelOffre = :idao ")
 AppelOffre  FindAOwithUsers(@Param("idao") Integer idao);

但它在查询中生成了双倍:

Hibernate: select appeloffre0_.ID_APPEL_OFFRE as col_0_0_, appeloffre0_.USER_SAISIE as col_1_0_, appeloffre0_.USER_CONTROLE as col_2_0_, appeloffre0_.USER_VALIDATION as col_3_0_ 
from ao.appel_offre appeloffre0_ 
left outer join ao.admin admin1_ on appeloffre0_.USER_SAISIE=admin1_.ID 
left outer join ao.admin admin2_ on appeloffre0_.USER_CONTROLE=admin2_.ID 
left outer join ao.admin admin3_ on appeloffre0_.USER_VALIDATION=admin3_.ID 
inner join ao.admin admin4_ on appeloffre0_.USER_SAISIE=admin4_.ID 
inner join ao.admin admin5_ on appeloffre0_.USER_CONTROLE=admin5_.ID 
inner join ao.admin admin6_ on appeloffre0_.USER_VALIDATION=admin6_.ID 
where appeloffre0_.ID_APPEL_OFFRE=?

而且我的构造函数中的System.out.println("users"); 也没有显示。

为什么这不起作用,我该如何解决这个问题

【问题讨论】:

    标签: mysql hibernate jpa spring-data


    【解决方案1】:

    构造函数没有被调用,因为查询没有返回任何结果。由于使用了内部连接,它没有返回任何结果,你是对的。要使其正常工作,请使用左连接,如下所示

    @Query(" select new AppelOffre(ao.idAppelOffre, us, uc, uv)  from AppelOffre  ao left join ao.userSaisie us left join ao.userControle uc left join ao.userValidation uv "
    

    【讨论】:

    • thakns 伙计,我已经尝试过左连接(我更新了我的问题),但它没有用,所以我使用别名 us, uc, uv 并且它有效 :)
    • 是的,如果你在select子句中使用ao.userSaisie,不管from子句中的左连接,SQL都会使用inner join。不客气。
    猜你喜欢
    • 2014-09-13
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    相关资源
    最近更新 更多