桌面 SWING 应用程序上的 jpa

发布于 2024-12-06 01:30:30 字数 1141 浏览 2 评论 0原文

我正在使用 SWING 开发一个单用户桌面应用程序。我对这种使用 java.sql api 的应用程序有一点经验,并发现它根本不舒服...

在我的新应用程序中,我尝试使用 JPA第一次,我阅读了很多教程,这些教程让我了解了几乎所有我需要的内容,但没有找到真正的 java 桌面应用程序的好例子。

我正在考虑使用遵循架构,但不知道我是否正确...

我考虑创建一个 MyPersistenceUnit 类:

    public class MyPersistenceUnit {
        private static EntityManagerFactory factory;
        private static EntityManager entityManager;

        public static void initiate(){
            factory=Persistence.createEntityManagerFactory("PU_Name");
            entityManager=factory.createEntityManager();
        }

        public static EntityManager getEntityManager() {
            return entityManager;
        }

        public static void close(){
            entityManager.close();
            factory.close();
        }

    }

启动()方法将是第一个被调用的,并且当应用程序关闭时将调用 close()方法。

当应用程序运行时,所有事务都将通过 getEntityManager() 实例完成,该实例可以在应用程序中的任何位置访问。如果我对 JSE 应用程序的理解是正确的,则获得的实体管理器具有扩展的持久性上下文,它将在实体管理器不关闭时将所有实体保持在托管状态,这就是让我这样想的原因...

我不知道我是否遗漏了一些东西,所以任何提示将不胜感激

请注意,我正在将 eclipselink 提供程序与 derby 嵌入式数据库一起使用。
谢谢

I'm developping a mono user desktop application using SWING. I had a little experience with this kind of application on which i used the java.sql api and figured out that it wasn't confortable at all ...

In my new application i'm trying to use JPA for the first time, i've read a lot of tutorials which made me understand almost all what i need, but didn't find a good example for real java Desktop applications.

I'm thinking of using the following architecture, but don't know if i'm right ...

i think of creating a MyPersistenceUnit class :

    public class MyPersistenceUnit {
        private static EntityManagerFactory factory;
        private static EntityManager entityManager;

        public static void initiate(){
            factory=Persistence.createEntityManagerFactory("PU_Name");
            entityManager=factory.createEntityManager();
        }

        public static EntityManager getEntityManager() {
            return entityManager;
        }

        public static void close(){
            entityManager.close();
            factory.close();
        }

    }

the initiate() method will be the first to be called, and the close() method will be called when the application gets closed.

While the application is running all transactions will be done through the getEntityManager() instance, which is accessible every where in the application. If my understanding is right on JSE applications the obtained entity manager has an extended persistence context which will keep all the entities on the managed state while the entity manager doesn't get closed, and that's what made me think this way ...

I don't know if i'm missing something, so any tip will be appreciated

Note that i'm using eclipselink provider with the derby embedded database.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

无人接听 2024-12-13 01:30:30

据我了解,问题归结为您是否应该打开 EntityManager 并全局存储其引用并在应用程序中的各处访问相同的实例。

我认为如果您的应用程序是中小型的,那应该没问题。请注意,数据库连接(因此会话/entityManager)可能会由于各种因素而下降。并且不要对事务执行此操作(即不要在开始时打开它们并在结束时提交)。保持交易尽可能细粒度。

有经验丰富的人对此进行了各种讨论,您可以在这里关注:对于反驳对此问题的争论 - 在 Swing 中使用 Hibernate 进行会话管理应用程序

另请参阅 关于同一主题。

这里是一个由 hibernate 提交者创建的示例桌面应用程序。它有点老了,你可以明白这个想法。

最后,这篇是一篇很棒的文章,有助于理解 JPA 的一般概念桌面应用程序。

As I understand, the question boils down to whether you should open EntityManager and store its reference globally and access the same instance everywhere in the application.

I think that should be okay if your application is small to medium size. Just be cautious that database connection (hence session/entityManager) may drop due to various factors. And don't do this with transactions (ie dont open them in beginning and commit in end). Keep transactions as fine grained as possible.

There have been various discussion where more experienced people discussed about it, you can follow that here : for and counter argument on this SO question - Session management using Hibernate in a Swing application

Also see this on the same topic.

Here is a sample desktop application created by a committer of hibernate. Its bit old, you can get the idea.

And finally this is great article for understanding of general JPA concepts for desktop application.

听风吹 2024-12-13 01:30:30

在重新思考我的设计之后,我决定对其进行如下更改:

  • 在应用程序启动时创建一个“永久”EM,并保持其打开状态直到应用程序关闭。 permanentEM 将用于在需要时查找/刷新实体(例如用于获取惰性关系......)。
    为了确保通过 WEAK 引用模式有效管理内存,我将避免永久引用 permanentEM 的托管实体。
  • 创建一个“临时”EM 来加载启动应用程序所需的“永久”数据。数据加载后,关闭该临时 EM,以分离所有加载到内存中的数据。
  • 为每个持久/合并/删除事务创建一个新的“临时”EM,并在事务提交后关闭它。

After re-thinking my design, i decided to change it as follows:

  • create a 'permanent' EM when the application starts, and keep it open until the application shutdowns. permanentEM will be used to find/refresh entities when needed (for fetching lazy relationships for example ...).
    In order to ensure an efficient management of memory by the WEAK refrence-mode, i'll avoid to permanently reference permanentEM's managed entities.
  • create a 'temporary' EM to load the 'permanent' data, that are necessary for the start of the application. Once the data loaded close that temporary EM, to detach all the loaded-in-memory data.
  • create a new 'temporary' EM, for each persist/merge/remove transaction, and close it once the transaction commits.
匿名。 2024-12-13 01:30:30

由于它是 SE 应用程序,您可以使用嵌入式数据库。
我知道H2通常是允许单线程访问的,所以如果你们在同一个开发环境中,并且想避免令人头疼的实体并发控制。

我的建议是将您的服务调用封装在一个线程中,并为该线程分配一个实体管理器。

不允许其他线程使用实体,然后定义数据访问接口,该接口可以以适当的数据结构将数据从实体传输到其他对象。

在一个世界中,我认为最好将所有实体放在一个实体管理器域中并将它们与其他线程隔离。

Since its a SE application, your may use the embedded database.
I know H2 normally allow one thread visit, so if your are in the same developing environment and want to avoid the headache concurrence control of entity.

my suggestion is that encapsulate the your service call in one thread and assign this thread a entitymanager.

don't allow other threads to use entities , then define data access interface which can transfer data from entity to other objects in a proper data structure .

in one world, i think it is better to put all the entities in one entity manager domain and isolate them from other thread.

萌能量女王 2024-12-13 01:30:30

你看过http ://docs.jboss.org/hibernate/core/4.0/manual/en-US/html/transactions.html#transactions-basics-issues。我认为在继续使用会话对象太多时间之前阅读那里的文档是明智的。我想更好的方法是使用会话来执行简单的工作单元(例如:CRUD 操作)。

希望有帮助!

问候。

have you looked at http://docs.jboss.org/hibernate/core/4.0/manual/en-US/html/transactions.html#transactions-basics-issues. I think it would be wise to read the documentation there before proceeding using sessions object for too much time. I guess the better approach is to use the session to do a simple unit of work (for example : the CRUD operations).

Hope it helps!!.

Greetings.

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