Hql子句在子句中是null,不像在mysql中那样工作

时间:2014-09-02 13:23:49

标签: java mysql hql

我在mysql中做了一个查询。

SELECT COALESCE(SUM(t2.ammout * CASE WHEN t2.idAccountDeb = :accountSelec AND  
(t2.idAccountDeb <> t2.idAccountCred OR t2.idAccountCred IS NULL) 
THEN 1 WHEN t2.idAccountCred = :accountSelec AND
(t2.idAccountDeb <> t2.idAccountCred OR t2.idAccountDeb IS NULL) THEN
-1 ELSE 0 END),0) FROM TransactionAccount t2 WHERE 
(t2.idAccountDeb = :accountSelec OR t2.idAccountCred = :accountSelec) AND
t2.dtInf <= :dateSelec 

我尝试在Hql中转换

SELECT COALESCE(SUM(t2.ammout * CASE WHEN t2.accountDeb = :accountSelec AND 
(t2.accountDeb <> t2.accountCred OR t2.accountCred IS NULL) THEN 1
WHEN t2.accountCred = :accountSelec AND (t2.accountDeb <> t2.accountCred OR
t2.accountDeb IS NULL) THEN -1 ELSE 0 END),0) FROM TransactionAccount t2 
WHERE   (t2.accountDeb = :accountSelec OR t2.accountCred = :accountSelec) 
AND t2.dtInf <= :dateSelec 

但是在hql中返回0.00,这是END),0)。在mysql中,150.97。

当帐户借记或帐户余额为空时,会发生这种情况。

我做错了什么? 例: 如果事务包含一个值为null的帐户贷方或帐户借方,则查询无法在“is null”子句中捕获。

我的豆子:

    @Entity
    public class Account implements Serializable {
    @Id
    @GeneratedValue
    private long id;
    @Column(length = 40)
    private String cod;
    private boolean credit;
    private String name;//CTA
    @OneToOne
    @JoinColumn(name = "idCompany")
    private Company company;


    @Version
    private int version;

    //getter and setters
}

    @Entity
    public class Transaction implements Serializable {
    @Id
    @GeneratedValue
    private long id;
    private String history;
    @JoinColumn(name = "idAccountDeb")
    @OneToOne
    private Account accountDeb;
    @JoinColumn(name = "idAccountCred")
    @OneToOne
    private Account accountCred;
    private BigDecimal ammount;
    @Temporal(TemporalType.DATE)
    private Date dtInf;
    @Temporal(TemporalType.TIMESTAMP)
    private Date dtPosted;
    @Temporal(TemporalType.TIMESTAMP)
    private Date dtModif;  
    private TypeTransaction type;
    @Version
    private int version;
}

1 个答案:

答案 0 :(得分:0)

我的解决方案。

将id放入比较帐户

新查询:

SELECT COALESCE(SUM(t2.ammount * CASE WHEN t2.accountDeb.id = :accountSelec 
           AND (t2.accountDeb.id <> t2.accountCred.id OR t2.accountCred.id is null) THEN 1
           WHEN  (t2.accountDeb.id <> t2.accountCred.id 
           OR t2.accountDeb.id is null) THEN -1 ELSE 0 END),0)
           FROM TransactionAccount t2 WHERE (t2.accountDeb = :accountSelec 
           OR t2.accountCred = :accountSelec) AND t2.dtInf <= :dateSelec