Grails+Db4o整合中的GenericObject问题

作者在尝试将Grails框架与db4o数据库进行整合时,在开发模式下遇到了对象类型转换的问题。通过将domain classes打包放入lib目录,解决了对象被误识别为GenericObject的问题,但这种做法牺牲了Grails的热更新特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在研究Grails跟db4o的整合。

 

昨天碰到这么一个奇怪的现象:

下面这段代码中的这句

ObjectSet os = getDb4oTemplate().get(new User(params))

在dev模式下一直报“... cannot be cast to com.db4o.reflect.generic.GenericObject ...”

而在prod模式就正常

 

class UserService extends Db4oDaoSupport {
    def objectContainer
    def login(params) {
      ObjectSet os = getDb4oTemplate().get(new User(params))
      if(os.size() > 0) {
        println "user found"
        params.session.user = os.get(0)
      } else {
        println "user not found"
      }
    }
}

 

而用SODA写query得到的是com.db4o.reflect.generic.GenericObject类的实例

 

//      ObjectSet os = getDb4oTemplate().get(new User(params))
      def tpl = getDb4oTemplate()
      Query q = tpl.query()
      q.constrain(User.class)
      q.descend('username').constrain(params.username)
      q.descend('password').constrain(params.password)
      ObjectSet os = q.execute()

为什么在dev模式下是GenericObject,而不是我自己的User呢?

 

我把dev跟prod两种模式下项目目录结构作了对比,有一个显著的区别,就是:前者有个target,下面放着grails/groovy编译得到的classes和其它一些东西;而后者是一标准的J2EE项目的结构,classes都在WEB-INF/classes下,其中就有我的domain class - User。

 

联想到“The Definitive Guide to Db4o”中的这段话:

The Definitive Guide to Db4o 写道
The object in Figure 5-5 has the symbol (G) next to it, which signifies that this object was
read by the generic reflector. This means that the ObjectManager doesn’t have access to the
original class definition. You can help the ObjectManager in displaying the object more naturally
(e.g., showing the real objects IDs) by providing the original C# or Java classes . The File ➤
Preferences menu option allows you to enter either the directory or the library (.dll or .jar)
files that contain the classes
.

虽然它是说Db4o的OM的,但道理应该是一样的。猜想它之所以不能转成User是因为它没看到我的User类。

那我就试着让User出现在它的视野中,试试这句

this.class.classLoader.rootLoader.addURL(new URL("file:///path/to/file"))

貌似无用。

 

换个方案,把domain classes打成jar包,拷贝到lib下,重启,...,问题解决了,不管是用QBE还是SODA,得到的都是User,而不再是GenericObject了!

 

但还是有个不方便的地方:每次我修改了domain class,都得重新打包,重新启动,损害了Grails开发的快速特性。

各位路过的朋友有没有完美的解决办法让它实现hot swapping?

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值