Java EE5 中的可配置持久性
有两种使用持久性单元的方法:代码或注释。
代码
[..]
EntityManagerFactory emf;
emf = Persistence.createEntityManagerFactory("SOMEPU");
[..]
或
注释
[..]
@PersistenceContext(name = "persistence/LogicalName", unitName = "SOMEPU")
[..]
问题:如果您想更改持久性单元(或指向不同的 jdbc 源),我可以轻松地调整源代码版本以从某些设置文件或其他文件中读取变量。 但我不能将变量放入注释中。 解决办法是什么?
是的,我可以始终保持相同的 PU,只需将应用程序服务器中的 jbdc 资源指向其他位置,但我不希望任何人在 AS 的管理设置中进行修改。
干杯 斯文
There are 2 ways of using a persistence unit, code or annotation.
CODE
[..]
EntityManagerFactory emf;
emf = Persistence.createEntityManagerFactory("SOMEPU");
[..]
or
ANNOTATION
[..]
@PersistenceContext(name = "persistence/LogicalName", unitName = "SOMEPU")
[..]
Question: If you want to change the persistence unit (or point to different jdbc source), I could easily adapt the sourcecode version to read a variable from some settings file or whatever. But I cant put variables into annotations. Whats is the solution ?
Yes, I could keep always the same PU and just point the jbdc resource in the applicationserver to somewhere else, but I dont want anyone to tinker around in the admin settings of the AS.
cheers
Sven
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您绝对必须使用注释来获取 PersistenceContext,那么我想您可以将 EntityManager 的创建包装在某个类中,然后将其注入到需要它的 bean 中?
然后在您的 EJB 中:
您提供的 MyPersistenceContext 实现如何创建 EntityManager 由您决定。
If you absolutely have to use annotations to get your PersistenceContext, then I guess you could wrap the creation of the EntityManager in some class and then have that injected into the bean that requires it?
And then in your EJB:
How the implementation of MyPersistenceContext you provide creates the EntityManager is up to you.