2011-12-09 43 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() 

仍然工程...

相关问题