简体   繁体   English

Grails CXF插件和GORM

[英]Grails CXF plugin and GORM

I'm using the CXF plugin version 2.0.1 and have a simple usage of GORM in a SOAP service method. 我正在使用2.0.1版的CXF插件,并在SOAP服务方法中简单使用了GORM。 When I run a test on the service, it throws an exception saying that it could not initialize a proxy because there was no session. 当我在服务上运行测试时,它引发异常,因为没有会话,它无法初始化代理。 According to some searches I did, this is a Hibernate issue which I am assuming means that GORM has not been properly initialized. 根据我进行的一些搜索,这是一个Hibernate问题,我认为这意味着GORM尚未正确初始化。 Could someone give me a pointer on how to initialize GORM manually within the endpoint class? 有人可以给我一个关于如何在端点类中手动初始化GORM的指针吗?

You're used to doing Hibernate work in controller actions (or methods called from there) and there's always an active Hibernate Session there because Grails uses the Open Session in View pattern, opening a session at the beginning of the request, binding it in a ThreadLocal so it can be conveniently accessed, and then flushing and closing the session at the end of the request. 您习惯于在控制器动作(或从那里调用的方法)中进行Hibernate工作,并且那里总是有一个活动的Hibernate Session,因为Grails使用Open View in View模式,在请求的开始处打开一个Session,将其绑定到一个ThreadLocal,以便可以方便地对其进行访问,然后在请求结束时刷新并关闭会话。 Some plugins do this also, for example the Quartz plugin uses job start/stop listeners to do the same thing so you can conveniently use GORM there just like in a controller. 一些插件也可以这样做,例如Quartz插件使用作业开始/停止侦听器执行相同的操作,因此您可以像在控制器中一样方便地在其中使用GORM。

The easiest way to ensure that a session is open for the duration of block of code is to use the static withTransaction method on any domain class. 确保在代码块期间打开会话的最简单方法是在任何域类上使用静态withTransaction方法。 It does start a transaction, so if you do database updates you'll also benefit from that, but even if you're only reading it is the way to go because the session will be open, and you'll be able to work with lazy-loaded 1-many and many-many references. 它确实会启动事务,因此,如果您进行数据库更新,您也会从中受益,但是即使您只阅读它,也仍然可以这样做,因为会话将打开,并且您可以使用延迟加载的1个和许多个引用。

The transaction/session have nothing to do with the domain class, so pick a random one and use it: 事务/会话与域类无关,因此随机选择一个并使用它:

User.withTransaction {

   def games = Game.findAllByFooAndBar(foo, bar)
   games.each { ... }
   ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM