2014-09-02 64 views
0

我在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。

发生这种情况时,帐户借方或帐户信用为空。

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

我豆类:

@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; 
} 
+0

我不知道,但我不认为这(例如)** THEN -1 ELSE 0 END **是有效的** HQL **(但当然,我可能是错的......) – Ben 2014-09-02 13:44:02

+0

如果交易包含一个帐户信用额度或帐户借方值为null,则查询无法在“null”子句中捕获。 – 2014-09-02 15:53:25

回答

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