Bonjour

J'ai implemente la persistance dans mon application(desktop) avec toplink.
J'ai eu beaucoup de mal a trouver des exemples sans ejb ou framework.
Donc je viens vers vous savoir si la maniere dont je m'y suis prise est correcte ou a revoir tout particulierement sur ma class EMProvider et ma maniere de recuperer l entitymanager dans l' abstract dao.


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class EMProvider {
 
    private static EntityManagerFactory emf = null;
 
    private static EntityManager em = null;
 
 
    private static String getDbUrl(){
         String dbName = Config.getProperty(ConfigConstants.KEY_DB_NAME);
         String dbPath = Config.getProperty(ConfigConstants.KEY_DB_DIR) + dbName; 
         return "jdbc:derby:" + dbPath;
    }
 
    public static EntityManagerFactory getEntityManagerFactory() {
 
        if( emf == null ) {
            Properties map = new Properties();
            map.put("toplink.jdbc.url", getDbUrl() );
            emf =  Persistence.createEntityManagerFactory("PU",map);
        }
       return emf;
    }
 
    public static EntityManager getEntityManager() {
 
        if( (em == null) || !em.isOpen() ){
            em = getEntityManagerFactory().createEntityManager();
            em.setFlushMode(FlushModeType.COMMIT);
        }
        return em;
    }
 
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
public abstract class Dao<K,E>
            implements IDao<K,E> {
 
    protected EntityManager em;
 
 
    public Dao() {
        em = EMProvider.getEntityManager();
    }

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
 
public class ApplicationDAO 
        extends Dao<Integer, Application>{
 
    public List<Application> getAll() {
        return em.createQuery("select a from Application a").getResultList();
    }
merci pour els reponses, conseils.