[Resolu] [Hibernate] : Erreur Could not find datasource
Bonjour les amis !!
J'utilise Hibernate 3.0, avec Netbeans 4,1 et son Tomcat integrer.
j ai cree un pool de connexion, et donc un acces JNDI, je teste avec une servlet, ca marche.
Deuxieme etape : faire joujou avec hibernate :
Je declare mon hibernate-cfg.xml comme ceci :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/test</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--Mapping files-->
<mapping resource="User_hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration> |
Puis je cree le fichier User.hbm.xml pour mapper ma pauvre table users avec un id,nom et prenom.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="User_hibernate.User" table="users">
<!-- l'ID de la table .-->
<id name="Id" type="int" column="Id" unsaved-value="0"><generator class="identity"/></id>
<!-- le nom -->
<property name="Nom"><column name="Nom"/></property>
<!-- le prenom -->
<property name="Prenom"><column name="Prenom"/></property>
</class>
</hibernate-mapping> |
Je cree un bean user avec les champs getters et setters,
Code:
public class User extends Object implements Serializable
Puis je cree la classe qui fait tout :
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
|
package User_hibernate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil
{
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static
{
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.out.println("ERROR");
log.error("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession()
{
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null)
{
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession()
{
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
} |
J ai trouver cet exemple dans un tutorial.
Pis dans la servlet, j appelle trankil
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
Session session;
Transaction tx;
//Creation de notre objet Session grace à notre HibernateUtil
session = HibernateUtil.currentSession();
//Ouverture de notre transaction avec Hibernate grace a la session
tx = session.beginTransaction();
// Ajout d'un utilisateur en utilisant notre bean User préalablement configuré dans Hibernate
User toto = new User();
toto.setNom("lateteatoto");
toto.setPrenom("toto");
// On sauve, on renvoi, notre bean à la session Hibernate
session.save(toto);
// Nous commitons la transaction vers la base
tx.commit();
//Enfin on ferme la session
HibernateUtil.closeSession(); |
Je detaille pour que vous puissiez bien m aider ;)
Et la j ai une erreur :
Code:
1 2 3 4 5
|
java.lang.ExceptionInInitializerError
at User_hibernate.HibernateUtil.<clinit>(HibernateUtil.java:26)
at Test.main(Test.java:21)
Caused by: org.hibernate.HibernateException: Could not find datasource |
ca craque ici
Code:
sessionFactory = new Configuration().configure().buildSessionFactory();
Ce qu est bizarre, c est que ca m insere une ligne des fois dans la database.
Bref, je comprends rien.
Si une ame charitable gourou d hibernate pouvait m aider !!
Merci Merci !!