OSGi +休眠 [英] OSGi + Hibernate

查看:92
本文介绍了OSGi +休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不是将数据库操作分散在四个(osgi)捆绑包中,而是在所有这些方面做稍微不同的事情。我想创建一个负责所有持久性问题的(简单)OSGi包。我觉得这并不像听起来那么简单,因为每个bundle都有独特的类加载器。
所以我真正感激的是,如果有人知道这种问题的解决方案。

解决方案

(如果您使用的是Hibernate Annotations)



实体类加载器,当Hibernate包被通知注释类时。

然后在构建SessionFactory之前做类似的事情。

  ClassLoad cl = Thread.currentThread()。getContextClassLoader(); 
尝试{
Thread.currentThread()。setContextClassLoader(yourClassLoader);
factory = cfg.buildSessionFactory();
} finally {
Thread.currentThread()。setContextClassLoader(cl); //恢复原来的类加载器
}


Instead of having database actions scattered in four (osgi) bundles, all doing there slightly different things. I want to create a (simple) OSGi bundle that is responsible for all persistance issues. I feel that this is not as simple as it sounds because of the "unique classloader per bundle" thing. So what I really would appreciate is if someone knows the solution(s) to this kind of problem.

解决方案

(If you are using Hibernate Annotations)

Save all the Entities class loaders when the Hibernate bundle is informed about an annotated class.

Then do something like this before building your SessionFactory.

ClassLoad cl = Thread.currentThread().getContextClassLoader();
try {
 Thread.currentThread().setContextClassLoader(yourClassLoader);
 factory = cfg.buildSessionFactory(); 
}finally {
 Thread.currentThread().setContextClassLoader(cl);  // restore the original class loader
}

这篇关于OSGi +休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆