如何从 Grails 服务中访问 EntityManager (JPA + GAE)
我在从 grails 服务中访问 EntityManager 时遇到问题:
我的设置如下...
- 带有 AppEngine 和 GORM-JPA 插件的基本 Grails 应用程序
- 几乎所有内容的默认设置。我还没有接触过 resources.groovy、persistence.xml 等。
有些东西很好...
- 我只需添加“defentityManager”就可以从控制器内访问 EntityManager。
但是...
- 我有一个服务,我试图从中访问 EntityManager,但是,我收到“java.lang.IllegalStateException:EntityManager 已关闭!”例外。
插件是否在某处关闭了 EntityManager?我是否需要以某种方式更改 EntityManager 的范围?我是否需要更新一些 XML 文件以确保正确注入?
class GoogleCalendarService 实现 InitializingBean { 无效 afterPropertiesSet() { }
}def 实体管理器 公共 OAuthToken getAccessToken(用户 u) { //无法从这里访问entityManager entityManager.newQuery(...) //抛出IllegalStateException }
一个奇怪的注释: 由于某种原因,如果我在 Jetty 启动并运行时重新保存服务,则该服务只能访问 EntityManager 一次。如果我单击重新加载(并让控制器再次访问该服务),该服务将无法再访问 EntityManager...
I'm having trouble accessing the EntityManager from within a grails service:
My setup is as follows...
- Basic Grails application with the AppEngine and GORM-JPA plugins
- Default settings for pretty much everything. I haven't touched resources.groovy, persistence.xml etc.
Some things are good...
- I am able to access the EntityManager from within controllers by simply adding "def entityManager".
But...
- I have a service that I'm trying to access the EntityManager from, however, I get a "java.lang.IllegalStateException: EntityManager is already closed!" exception.
Is a plugin closing the EntityManager somewhere? Do I need to change the scope of the EntityManager somehow? Is there some XML file I need to update to ensure proper injection?
class GoogleCalendarService implements InitializingBean { void afterPropertiesSet() { }
def entityManager public OAuthToken getAccessToken(User u) { //can't access entityManager from here entityManager.newQuery(...) //throws an IllegalStateException }
}
One weird note:
For some reason, if I re-save the service while Jetty is up and running the service is able to access the EntityManager just once. If I click reload (and have the controller access the service again) the service can no longer access the EntityManager...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想正确的方法是使用 EntityManagerFactory,如下所示:
I guess the right way to do this is use the EntityManagerFactory, like this: