Java EE5 中的可配置持久性

发布于 2024-07-29 16:08:49 字数 534 浏览 6 评论 0原文

有两种使用持久性单元的方法:代码或注释。

代码
[..]
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒与心事 2024-08-05 16:08:49

如果您绝对必须使用注释来获取 PersistenceContext,那么我想您可以将 EntityManager 的创建包装在某个类中,然后将其注入到需要它的 bean 中?

public interface MyPersistenceContext
{
      public void getEntityManager();
}

然后在您的 EJB 中:

public class MyEJB
{

      @EJB
      private MyPersistenceContext persistenceContext;

      private EntityManager em;

      @PostConstruct
      public void postConstruct()
      {
             em = persistenceContext.getEntityManager();
      }

 ....

您提供的 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?

public interface MyPersistenceContext
{
      public void getEntityManager();
}

And then in your EJB:

public class MyEJB
{

      @EJB
      private MyPersistenceContext persistenceContext;

      private EntityManager em;

      @PostConstruct
      public void postConstruct()
      {
             em = persistenceContext.getEntityManager();
      }

 ....

How the implementation of MyPersistenceContext you provide creates the EntityManager is up to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文