gpt4 book ai didi

hibernate - 在域类初始化中使用编码器

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

有趣的问题:

我有一个域类,看起来像

class Dummy {
String key = (''+new Date()).encodeAsSHA256()
}

当我尝试启动我的应用程序时,出现了很长的堆栈跟踪/错误消息,指出
Invocation of init method failed; nested exception is org.hibernate.InstantiationException: could not instantiate test objectmad.Dummy
...
Caused by MissingMethodException: No signature of method: java.lang.String.encodeAsSHA256() is applicable for argument types: () values: []

现在,如果我删除 .encodeAsSHA256(),以开发人员模式启动应用程序并重新插入编码,那么它就可以工作了:-)

因此,基本上,代码在运行时有效,但似乎在初始化时,String类尚未为编码器准备好。

知道如何在不创建自定义构造函数的情况下解决此问题吗?

最佳答案

找到了一个解决方案:编码器也可以直接调用,但是您必须知道正确的程序包...

import org.codehaus.groovy.grails.plugins.codecs.SHA256Codec

class Dummy {
String key = SHA256Codec.encode(''+new Date())
}

...解决了问题...

关于hibernate - 在域类初始化中使用编码器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12715061/

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