slt,j'essai de faire une connexion Mysql/hibernate mai il y a une erreur qui s'affiche:
javax.naming.NameNotFoundException: Le Nom HibernateSessionFactory n'est pas lié à ce Contexte
Version imprimable
slt,j'essai de faire une connexion Mysql/hibernate mai il y a une erreur qui s'affiche:
javax.naming.NameNotFoundException: Le Nom HibernateSessionFactory n'est pas lié à ce Contexte
aurrais tu fais une erreur de frappe en tappant une majuscule pour ta session factory
sinon un example de conf http://www.kodejava.org/examples/245.html
nn j'pense pas voici le code de l'hibernate.cfg.xml:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.url">jdbc:mysql://localhost/base</property> <property name="connection.username">root</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> <!-- thread is the short name for org.hibernate.context.ThreadLocalSessionContext and let Hibernate bind the session automatically to the thread --> <property name="current_session_context_class">thread</property><!-- this will show us all sql statements --> <property name="hibernate.show_sql">true</property> <mapping resource="com/yourcompany/hibernate/Hôtels.hbm.xml" /> <mapping resource="com/yourcompany/hibernate/Restaurants.hbm.xml" /> <mapping resource="com/yourcompany/hibernate/Hôpitaux.hbm.xml" /> </session-factory> </hibernate-configuration>
Tu n'as pas l'erreur complète ?
Ce n'est sans doute pas cela, mais tu dois renseigner le password de ta base de données dans ton fichier de config.
Ce code pose souvent un pb, ces jours ci 8O
Manque tjs le mot de passe de la connexion même si l'exception n'a pas l'air d'être à cause de celà ..
Et aussi le localhost:3306
mai j"ai pa utisé un mot de passe
re en faite je croi ke l'erreur s'yavien du openSession factory
Code:
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.yourcompany.struts.action; import java.util.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import com.yourcompany.struts.form.AcceuilsFB; /** * MyEclipse Struts * Creation date: 12-31-2008 * * XDoclet definition: * @struts.action validate="true" */ public class AcceuilsACTAction extends DispatchAction { /* * Generated Methods */ /** * Method execute * @param mapping * @param form * @param request * @param response * @return ActionForward */ public ActionForward HOTEL(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { // TODO Auto-generated method stub AcceuilsFB fb= (AcceuilsFB) form; //ActionForward forward = new ActionForward(); //ajouter Simplelbs //ajouter veiwlbs //ajouter cordx cordy String coordx; String coordy ; SimpleLBS myLbs = new SimpleLBS(); Positione p=new Positione(); myLbs.viewLocation("22528837"); coordx=myLbs.getX(); coordy=myLbs.getY(); p.setx(""+coordx); p.sety(""+coordy); double x =p.Convertx(); double y =p.Converty(); double xmin=x-0.005; double ymin=y-0.005; double xmax=x+0.005; double ymax=y+0.005; fb.setCordx(""+x); // Mettre le résulat sur l'un des ces faviable fb.setCordy(""+y); fb.setAdress("adresse hotel"); // CONNEXION BASE DONN2E Transaction tx = null; Session session = null; try { SessionFactory sessionFactory = null; try { Context ctx = new InitialContext(); Object obj = ctx.lookup("HibernateSessionFactory"); sessionFactory =(SessionFactory) obj; } catch (NamingException e1) { e1.printStackTrace(); } session = sessionFactory.openSession(); String Hql=" select adresse,télephone from Hôtels " ; //" where x between " +xmin+" AND"+xmax+ //" AND y between" +ymin+"AND"+ymax ; Query q= session.createQuery(Hql); List list = q.list(); Iterator it = list.iterator(); while (it.hasNext()){ AcceuilsFB tempfb = new AcceuilsFB(); if(it.hasNext()) { Object[] tempobj=(Object[]) it.next(); tempfb = new AcceuilsFB() ; String adress=(tempobj[0]!= null ? tempobj[0].toString(): ""); String num=(tempobj[1]!= null ? tempobj[1].toString(): ""); tempfb.setAdress("adresse"+adress+"num"+num); } }} catch (HibernateException e1) { e1.printStackTrace(); if (tx != null && tx.isActive()) tx.rollback(); System.out.println("Echec " + e1.getMessage()); } catch (Exception e) { } finally { return mapping.findForward("acceuilsFB"); } }
En passant, avoir des noms de tables de mappings et de classes avec des caractères non ascii (genre ô dans hôtels) est une très mauvaise idée.