2014-09-02 47 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