gpt4 book ai didi

grails - 休眠:强制get()遵守其参数的数据类型

转载 作者:行者123 更新时间:2023-12-02 14:39:34 34 4
gpt4 key购买 nike

设置:Grails 2.5.6和Hibernate 4.3.10

我有一个带有字符串ID的表。事实是,它的值是数字字符串,当我传入诸如get()这样的值时,这似乎使"000000"变得混乱。

域类:

class Term
{
static mapping = {
id name: 'code', generator: 'assigned'
version false
code column: 'CODE'
description column: 'DESC'
}

String code
String description
}

数据如下:
CODE   || DESC
-------++---------------------------
000000 || The Beginning of Time
201715 || Post Secondary Winter 2017
201815 || Post Secondary Winter 2018
999999 || The End of Time

然后在测试中,我发现了以下内容:
assert Term.list()          // works fine
assert !Term.get('foo') // works fine
//assert Term.get('000000') // throws exception

抛出的异常是:
Method threw 'org.springframework.orm.hibernate4.HibernateSystemException' exception.
Provided id of the wrong type for class Term. Expected: class java.lang.String, got class java.lang.Long
org.hibernate.TypeMismatchException: Provided id of the wrong type for class Term. Expected: class java.lang.String, got class java.lang.Long

所以看起来在某些时候'000000'和'201715'以及其他任何东西都被不方便地转换为Long对象。使用 as String也无济于事。谁能帮我告诉Hibernate这个String应该被当作String?

最佳答案

这似乎是一个Grails错误,我猜是因为您没有在域类中声明idString类型,因为它映射到了另一个字段(这很有意义)。

您可以尝试添加

String id

到您的域类,尽管这可能不会导致列生成所需的行为。

我建议您使用 get()而不是 findByCode(),因为您已将ID映射到 code字段,并且结果应该相同。

关于grails - 休眠:强制get()遵守其参数的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47677294/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com