桌面SWING应用程序上的jpa [英] jpa on a Desktop SWING Application

查看:112
本文介绍了桌面SWING应用程序上的jpa的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ...

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

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 ...

我想创建一个MyPersistenceUnit类:

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();
        }

    }

initiant()方法将是第一个被调用的方法,close()方法将在应用程序关闭时被调用.

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

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

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

请注意,我正在使用eclipselink提供程序和derby嵌入式数据库.
谢谢

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

推荐答案

重新考虑设计之后,我决定如下进行更改:

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

  • 在应用程序启动时创建一个永久" EM,并使其保持打开状态,直到应用程序关闭.当需要时,permanentEM将用于查找/刷新实体(例如,获取懒惰的关系...).
    为了确保通过WEAK刷新模式对内存进行有效管理,我将避免永久引用permanentEM的托管实体.
  • 创建一个临时" EM来加载永久"数据,这对于启动应用程序是必需的.加载数据后,关闭该临时EM,即可分离所有已加载的内存数据.
  • 为每个持久/合并/删除事务创建一个新的临时" EM,并在事务提交后将其关闭.
  • 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.

这篇关于桌面SWING应用程序上的jpa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆