2013-03-19 233 views
0

當我試圖設置在某個特定的Hibernate查詢字符串值,但我收到以下錯誤:爲什麼我得到這個錯誤?

java.sql.SQLDataException: An attempt was made to get a data value of type 'VARBINARY' from a data value of type 'VARCHAR'.

以下是哪裏出了問題來了休眠代碼:

公開名單getTransactions(長cardnum中,字符串類型){

Query q1 = factory.getCurrentSession().createQuery("FROM bank.persistence.entity.Transactions WHERE PK_CRED_CARD_ID=:CardID AND TRANSACTION_TYPE=:trType"); 
    q1.setParameter("CardID", cardNum); 
    q1.setParameter("trType", type); 
    return q1.list(); 

} 

當我試圖設置在某個特定的Hibernate查詢字符串值,但我收到以下錯誤:

java.sql.SQLDataException: An attempt was made to get a data value of type 'VARBINARY' from a data value of type 'VARCHAR'.

以下是哪裏出了問題來了休眠代碼:

公開名單getTransactions(長cardnum中,字符串類型){

Query q1 = factory.getCurrentSession().createQuery("FROM bank.persistence.entity.Transactions WHERE PK_CRED_CARD_ID=:CardID AND TRANSACTION_TYPE=:trType"); 
    q1.setParameter("CardID", cardNum); 
    q1.setParameter("trType", type); 
    return q1.list(); 

} 

以下是交易類:

package bank.persistence.entity; 

    import java.io.Serializable; 
    import java.util.Date; 
    import javax.persistence.Column; 
    import javax.persistence.Entity; 
    import javax.persistence.GeneratedValue; 
    import javax.persistence.GenerationType; 
    import javax.persistence.Id; 
    import javax.persistence.JoinColumn; 
    import javax.persistence.ManyToOne; 
    import javax.persistence.SequenceGenerator; 
    import javax.persistence.Temporal; 
    import javax.persistence.TemporalType; 

    @Entity 
    public class Transactions implements Serializable { 

    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private int ID; 
    @Column(name = "TRANSACTION_TYPE") 
    private String transType; 
    @Column(name = "AMOUNT") 
    private float amt; 
    @Column(name = "DESCRIPTION") 
    private String desr; 
    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "TRANSACTION_DATE") 
    private Date tranDate; 
    @ManyToOne 
    @JoinColumn(name = "PK_ACCT_ID") 
    private Account account; 
    @ManyToOne 
    @JoinColumn(name = "PK_CRED_CARD_ID") 
    private CreditCard credCard; 

    public String getTransType() { 
     return transType; 
    } 

    public void setTransType(String transType) { 
     this.transType = transType; 
    } 

    public String getDesr() { 
     return desr; 
    } 

    public void setDesr(String desr) { 
     this.desr = desr; 
    } 

    public CreditCard getCredCard() { 
     return credCard; 
    } 

    public void setCredCard(CreditCard credCard) { 
     this.credCard = credCard; 
    } 

    public Account getAccount() { 
     return account; 
    } 

    public void setAccount(Account account) { 
     this.account = account; 
    } 

    public int getID() { 
     return ID; 
    } 

    public void setID(int ID) { 
     this.ID = ID; 
    } 

    public float getAmt() { 
     return amt; 
    } 

    public void setAmt(float amt) { 
     this.amt = amt; 
    } 

    public Date getTranDate() { 
     return tranDate; 
    } 

    public void setTranDate(Date tranDate) { 
     this.tranDate = tranDate; 
    } 
    }// Class ends here 
+0

尊敬的ktj,您是否檢查過您正在設置的參數是否爲'空'值? – Amol 2013-03-19 07:04:56

+0

@kjt - 檢查我的答案並嘗試一下。 – SudoRahul 2013-03-19 07:25:59

+0

接受解決您的問題的答案,以便知道此問題已得到解答。 – SudoRahul 2013-03-19 08:20:42

回答

0

我認爲數據類型加夫由你在實體和數據庫中的不同。使他們都是一樣的。 在實體使其爲String

@Column(name = "TRANSACTION_TYPE" type="String") 

我認爲cardnum中的一個,類型爲null

+0

繼以下部分交易分類:數據類型爲字符串 @Column(name =「TRANSACTION_TYPE」) private String transType; – ktj 2013-03-19 07:00:17

+0

@ktj嘗試一次 – PSR 2013-03-19 07:03:51

+0

**不工作** – ktj 2013-03-19 07:10:12

0

自HQL,應您的查詢是這樣的: -

Query q1 = factory.getCurrentSession().createQuery("FROM bank.persistence.entity.Transactions T 
WHERE T.credCard.id=:CardID AND T.transType=:trType"); 

你需要給出映射到列的變量。不是直接列名稱。 我假設CreditCard表中的ID列爲id。如果不是,請將其更改爲任何相關的內容。

0

如果所有類型都比檢查setParameter()中設置的值是否爲null,在這種情況下,hibernate將拋出此錯誤。

相關問題