Bonjour,
Je voudrais connaitre l'utilité de générer des classe DAO avec hibernate tools. Étant novice dans le domaine, je demande votre aide pour mieux comprendre.
j'ai généré avec hibernate tools: UtilsateurHome= UtilisateurDAO comme suit:
Puis il y la classe d'encapsulation Utilisateur :
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 package com.hibernate.dao; // Generated 5 janv. 2013 19:54:10 by Hibernate Tools 3.4.0.CR1 import java.util.List; import javax.naming.InitialContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.LockMode; import org.hibernate.SessionFactory; import org.hibernate.criterion.Example; import com.hibernate.pojo.Utilisateur; /** * Home object for domain model class Utilisateur. * @see com.hibernate.dao.Utilisateur * @author Hibernate Tools */ public class UtilisateurHome { private static final Log log = LogFactory.getLog(UtilisateurHome.class); private final SessionFactory sessionFactory = getSessionFactory(); protected SessionFactory getSessionFactory() { try { return (SessionFactory) new InitialContext() .lookup("SessionFactory"); } catch (Exception e) { log.error("Could not locate SessionFactory in JNDI", e); throw new IllegalStateException( "Could not locate SessionFactory in JNDI"); } } public void persist(Utilisateur transientInstance) { log.debug("persisting Utilisateur instance"); try { sessionFactory.getCurrentSession().persist(transientInstance); log.debug("persist successful"); } catch (RuntimeException re) { log.error("persist failed", re); throw re; } } public void attachDirty(Utilisateur instance) { log.debug("attaching dirty Utilisateur instance"); try { sessionFactory.getCurrentSession().saveOrUpdate(instance); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public void attachClean(Utilisateur instance) { log.debug("attaching clean Utilisateur instance"); try { sessionFactory.getCurrentSession().lock(instance, LockMode.NONE); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public void delete(Utilisateur persistentInstance) { log.debug("deleting Utilisateur instance"); try { sessionFactory.getCurrentSession().delete(persistentInstance); log.debug("delete successful"); } catch (RuntimeException re) { log.error("delete failed", re); throw re; } } public Utilisateur merge(Utilisateur detachedInstance) { log.debug("merging Utilisateur instance"); try { Utilisateur result = (Utilisateur) sessionFactory .getCurrentSession().merge(detachedInstance); log.debug("merge successful"); return result; } catch (RuntimeException re) { log.error("merge failed", re); throw re; } } public Utilisateur findById(int id) { log.debug("getting Utilisateur instance with id: " + id); try { Utilisateur instance = (Utilisateur) sessionFactory .getCurrentSession().get("com.hibernate.dao.Utilisateur", id); if (instance == null) { log.debug("get successful, no instance found"); } else { log.debug("get successful, instance found"); } return instance; } catch (RuntimeException re) { log.error("get failed", re); throw re; } } public List findByExample(Utilisateur instance) { log.debug("finding Utilisateur instance by example"); try { List results = sessionFactory.getCurrentSession() .createCriteria("com.hibernate.dao.Utilisateur") .add(Example.create(instance)).list(); log.debug("find by example successful, result size: " + results.size()); return results; } catch (RuntimeException re) { log.error("find by example failed", re); throw re; } } }
Enfin, j'ai mi en place une classe de test:
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 package com.hibernate.pojo; import java.util.HashSet; import java.util.Set; public class Utilisateur implements java.io.Serializable { private int idutilisateur; private String nom; private String prenom; private String email; private String pw; private String adresse; private boolean confirme; private boolean temporaire; private Set paniers = new HashSet(0); public Utilisateur() { } public Utilisateur(int idutilisateur, String nom, String prenom, String email, String pw, String adresse, boolean confirme, boolean temporaire) { this.idutilisateur = idutilisateur; this.nom = nom; this.prenom = prenom; this.email = email; this.pw = pw; this.adresse = adresse; this.confirme = confirme; this.temporaire = temporaire; } public Utilisateur(int idutilisateur, String nom, String prenom, String email, String pw, String adresse, boolean confirme, boolean temporaire, Set paniers) { this.idutilisateur = idutilisateur; this.nom = nom; this.prenom = prenom; this.email = email; this.pw = pw; this.adresse = adresse; this.confirme = confirme; this.temporaire = temporaire; this.paniers = paniers; } public int getIdutilisateur() { return this.idutilisateur; } public void setIdutilisateur(int idutilisateur) { this.idutilisateur = idutilisateur; } public String getNom() { return this.nom; } public void setNom(String nom) { this.nom = nom; } public String getPrenom() { return this.prenom; } public void setPrenom(String prenom) { this.prenom = prenom; } public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } public String getPw() { return this.pw; } public void setPw(String pw) { this.pw = pw; } public String getAdresse() { return this.adresse; } public void setAdresse(String adresse) { this.adresse = adresse; } public boolean isConfirme() { return this.confirme; } public void setConfirme(boolean confirme) { this.confirme = confirme; } public boolean isTemporaire() { return this.temporaire; } public void setTemporaire(boolean temporaire) { this.temporaire = temporaire; } public Set getPaniers() { return this.paniers; } public void setPaniers(Set paniers) { this.paniers = paniers; } }
Donc, j'ai suivi quelques tuto sur le net comme http://docs.jboss.org/tools/OLD/3.3....refeng_codegen, malheureusement, je ne voit pas l'utilisation des classes DAO dans leurs codes, et l'utilité de leurs présences sur le projet.
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
34
35
36
37
38
39
40
41
42
43 /* Cette classe est dédié au test de l'API criteria */ //on doit avoir l'ensemble des users présents dans la BDD package com.test; import java.util.Iterator; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.hibernate.pojo.*; public class Test6JanvierCriteria { public static void main(String args[]) { Configuration config = new Configuration(); SessionFactory sessionFactory = config.configure().buildSessionFactory(); Transaction transaction = null; List personnes = null; Session session = sessionFactory.openSession(); try { transaction = session.beginTransaction(); personnes = session.createCriteria(Utilisateur.class).list(); System.out.println("nbUsers = " + personnes.size()); Iterator it = personnes.iterator(); while (it.hasNext()) { Utilisateur personne = (Utilisateur) it.next(); System.out.println("User : " + personne.getNom() +" "+ personne.getPrenom()); System.out.println(" Adresse : " + personne.getAdresse()); } transaction.commit(); } catch (Exception e) { transaction.rollback(); e.printStackTrace(); } finally { session.close(); } sessionFactory.close(); } }
Merci d'avance de votre aide![]()
Partager