简体   繁体   English

桌面SWING应用程序上的jpa

[英]jpa on a Desktop SWING Application

I'm developping a mono user desktop application using SWING. 我正在使用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 ... 我对使用java.sql api的这种应用程序有一点经验,并且发现它根本不舒服......

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. 在我的新应用程序中,我第一次尝试使用JPA,我已经阅读了很多教程,这些教程让我理解了我所需要的几乎所有内容,但是没有找到真正的Java桌面应用程序的好例子。

I'm thinking of using the following architecture, but don't know if i'm right ... 我正在考虑使用以下架构,但不知道我是否正确......

i think of creating a MyPersistenceUnit class : 我想创建一个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();
        }

    }

the initiate() method will be the first to be called, and the close() method will be called when the application gets closed. activate()方法将是第一个被调用的方法,并且当应用程序关闭时将调用close()方法。

While the application is running all transactions will be done through the getEntityManager() instance, which is accessible every where in the application. 在应用程序运行时,所有事务都将通过getEntityManager()实例完成,该实例可在应用程序的每个位置访问。 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 ... 如果我对JSE应用程序的理解是正确的,那么获得的实体管理器有一个扩展的持久化上下文,它将使所有实体保持在托管状态,而实体管理器不会被关闭,这就是让我这样思考的原因......

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. 请注意,我正在使用eclipselink提供程序和derby嵌入式数据库。
Thanks 谢谢

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. 据我了解,问题归结为您是否应该打开EntityManager并全局存储其引用并在应用程序中的任何位置访问相同的实例。

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 已经有各种讨论,其中比较有经验的人了解它的讨论,你可以按照在这里: 对于计数器在这太问题的说法- 在Swing应用程序使用Hibernate的Session管理

Also see this on the same topic. 也可以在同一主题上看到这一点

Here is a sample desktop application created by a committer of hibernate. 是一个由hibernate提交者创建的示例桌面应用程序。 Its bit old, you can get the idea. 它有点旧,你可以得到这个想法。

And finally this is great article for understanding of general JPA concepts for desktop application. 最后, 是了解桌面应用程序的一般JPA概念的好文章。

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. 在应用程序启动时创建一个“永久”EM,并在应用程序关闭之前保持打开状态。 permanentEM will be used to find/refresh entities when needed (for fetching lazy relationships for example ...). permanentEM将用于在需要时查找/刷新实体(例如,用于获取延迟关系...)。
    In order to ensure an efficient management of memory by the WEAK refrence-mode, i'll avoid to permanently reference permanentEM's managed entities. 为了确保通过WEAK参考模式有效地管理内存,我将避免永久引用永久管理的管理实体。
  • create a 'temporary' EM to load the 'permanent' data, that are necessary for the start of the application. 创建一个“临时”EM来加载启动应用程序所必需的“永久”数据。 Once the data loaded close that temporary EM, to detach all the loaded-in-memory data. 一旦数据加载关闭临时EM,分离所有加载的内存数据。
  • create a new 'temporary' EM, for each persist/merge/remove transaction, and close it once the transaction commits. 为每个持久化/合并/删除事务创建一个新的“临时”EM,并在事务提交后关闭它。

Since its a SE application, your may use the embedded database. 由于它是SE应用程序,您可以使用嵌入式数据库。 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. 我知道H2通常允许一个线程访问,所以如果你在同一个开发环境中并且想避免实体的头痛并发控制。

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. 在一个世界中,我认为最好将所有实体放在一个实体管理器域中并将它们与其他线程隔离开来。

have you looked at http://docs.jboss.org/hibernate/core/4.0/manual/en-US/html/transactions.html#transactions-basics-issues . 你看过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). 我想更好的方法是使用会话来完成一个简单的工作单元(例如:CRUD操作)。

Hope it helps!!. 希望能帮助到你!!。

Greetings. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM