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