JDO 与 Java EE 6(CDI 和 JTA?)
如果这是一个显而易见的问题,我很抱歉,但我正在阅读几本书的同时浏览 Java EE 6 教程,并且很难将所有信息关联起来。
我正在对 JDO 和 JPA 进行一些比较。据我所知,使用 JPA 和应用程序服务器,我可以很容易地说:
@Stateless
public class MyEJB {
@PersistenceContext
private EntityManager em;
// methods that use the JPA entity manager...
}
然后,在我自己的方法中,我可以使用 em
来获取 JPA EntityManager。无论我编写什么方法,都会(默认情况下)自动创建或加入现有事务。
我想享受 JDO 的乐趣。我怀疑正确的答案是使用 CDI。我不确定那会是什么样子,也许是这个?
@Stateless
public class MyEJB {
@Resource
private PersistenceManager em;
// methods that use the JDO persistence manager...
}
但这种猜测给我留下的问题多于答案。
- 我如何告诉 Glassfish 或无论我的 Java EE 6 应用程序服务器是什么,如何创建 PersistenceManagerFactory 以及如何使用它为我生成 PersistenceManager?
- 我需要做什么特别的事情才能实现 JTA 吗?如果可能的话,我希望使用容器管理的事务。
- 我可以将其设置为使用 JNDI 来查找我的 JDBC 连接吗?
- 是否需要存在神奇的文件才能触发我想要的行为? (我正在看着你,空的 persistence.xml)
除了直接强加对 JDO 和可能的 DataNucleus 的依赖之外,我宁愿将其尽可能保留为 Java EE 6,而不涉及 Spring 或其他第三方库,但我会使用第三方库比什么都重要。
谢谢!
I apologize if this is an obvious question, but I'm going through the Java EE 6 tutorial while reading a couple books and it's getting hard to correlate all of the information.
I'm doing a little comparison between JDO and JPA. I understand that with JPA and an application server, I can quite easily say something like:
@Stateless
public class MyEJB {
@PersistenceContext
private EntityManager em;
// methods that use the JPA entity manager...
}
Then, within my own methods, I can use em
to get at a JPA EntityManager. Whatever methods I write will (by default) automatically create or join up with an existing transaction.
I'd like to have this much fun with JDO. I suspect the right answer is to use CDI. I'm not sure what that would look like though, perhaps this?
@Stateless
public class MyEJB {
@Resource
private PersistenceManager em;
// methods that use the JDO persistence manager...
}
But this guesswork leaves me with more questions than answers.
- How do I tell Glassfish or whatever my Java EE 6 application server turns out to be, how to make the PersistenceManagerFactory and how to use it to generate PersistenceManagers for me?
- Do I have to do anything special to achieve JTA? I'd like to be using container managed transactions if possible.
- Can I set this up to use JNDI to find my JDBC connection?
- Are there magical files that need to exist to trigger the behavior I want? (I'm looking at you, empty persistence.xml)
Apart from imposing a dependency on JDO and probably DataNucleus directly I'd rather keep this as Java EE 6 as possible, without involving Spring or other third party libraries, but I'd take a third party library over nothing.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://www.datanucleus.org/products/accessplatform_3_0/jdo/j2ee.html
涵盖了 Java EE 的许多方面,并给出了包括 JBoss 7(最新规范)在内的多个 Java EE 服务器的示例。这是一项贡献性的工作,因为没有人使用所有此类服务器;如果您需要添加详细信息,请将其发布到 DataNucleus 论坛上,并且可以将其包含在内
http://www.datanucleus.org/products/accessplatform_3_0/jdo/j2ee.html
covers many aspects of Java EE, and gives examples for several Java EE servers including JBoss 7 (the latest spec). It is a contributory effort since nobody uses all such servers; if you have details to add then post them on the DataNucleus forum and they can be included