Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jpa on a Desktop SWING Application

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

like image 780
George Casttrey Avatar asked Sep 20 '11 21:09

George Casttrey


People also ask

Can we use hibernate with desktop application?

SessionFactory is the best way to use Hibernate in any(web based or Desktop) Application. Just download latest hibernate release bundle from http://sourceforge.net/projects/hibernate/files/hibernate4/. There's a required folder in it, under lib folder.

Which API is created with the help of persistence unit during the application?

The Java™ Persistence API (JPA) provides a mechanism for managing persistence and object-relational mapping and functions since the EJB 3.0 specifications. The JPA specification defines the object-relational mapping internally, rather than relying on vendor-specific mapping implementations.

Why do we use JPA with Hibernate?

Hibernate is an implementation of JPA. Hence, the common standard which is given by JPA is followed by Hibernate. It is a standard API that permits to perform database operations. It is used in mapping Java data types with SQL data types and database tables.


2 Answers

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.

like image 80
kdabir Avatar answered Oct 03 '22 10:10

kdabir


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.
like image 31
George Casttrey Avatar answered Oct 03 '22 09:10

George Casttrey