2011-12-09 70 views
4

我正在使用Play!一個小應用程序的框架。在模型中,我有以下查詢:爲什麼需要爲字符串Hibernate JPQL類型轉換

public static ApplicationUser getByUserName(String userName) { 
    return ApplicationUser.find("SELECT u FROM ApplicationUser u WHERE u.userName = ?", userName).first(); 
} 

這與完全在內存數據庫H2但是當我使用Postgres的我得到以下錯誤:

22:57:10,371 WARN ~ SQL Error: 0, SQLState: 42883 
22:57:10,371 ERROR ~ ERROR: operator does not exist: character varying = bytea 
    Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts. 
    Position: 167 

當我投的參數,如:

ApplicationUser.find("SELECT u FROM ApplicationUser u WHERE u.userName = CAST(? AS string)", userName).first() 

然後它工作。但爲什麼這是必要的。這可能是一個Hibernate的錯誤?

更新: 我將播放版本從1.2.4降級到1.2.3,現在它可以正常工作。我認爲這個問題可能在於運送postgres jdbc驅動程序。

更新II:問題仍未解決。我再次得到了同樣的錯誤查詢:

ApplicationRole.find("byName", name).first(); 

錯誤:

JPAQueryException occured : Error while executing query SELECT u FROM ApplicationUser u WHERE u.userName = ?: ERROR: operator does not exist: character varying = bytea Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts. 

application.conf我有:具有此錯誤

jpa.dialect=org.hibernate.dialect.PostgreSQLDialect

沒人?

回答

2

嘗試更換? (?1)通過爲:

public static ApplicationUser getByUserName(String userName) { 
    return ApplicationUser.find("SELECT u FROM ApplicationUser u WHERE u.userName = (?1)", userName).first(); 
} 

我有一個類似的查詢和工作得很好。我假設領域的userName在ApplicationUser的類型爲String(以防萬一!)

而且,多米尼克建議,請檢查您jpa.dialect設置爲自動發現或PosgreSQL。

+0

看我的更新。我已經從1.2.4降級到1.2.3,現在一切正常。 – reen

+0

現在幫助了我。謝謝。抱歉回覆晚了 – reen

4

你在conf/application.conf中啓用了postgresql數據庫方言嗎?

jpa.dialect = org.hibernate.dialect.PostgreSQLDialect

+0

查看我的更新。我已經從1.2.4降級到1.2.3,現在一切正常。 – reen

1

我有完全相同的問題,1.2.4。

User.find("byEmail", email).first() 

獲取完全相同的鑄造的錯誤你,所以我有(感謝佩爾)

User.find("email = (?1)", email).first() 

有趣的是,以取代它,

User.find("byEmailAndPassword", email, password).first() 

仍然工程...

相關問題