Hibernate && NetBeans
yassienrt
Messages postés
31
Statut
Membre
-
lin -
lin -
Bonjour,
j'arrive pas a faire marcher hibernate, voila l'erreur ki me donne :
voici ma configuration :
le fichier hibernate.cfg.xml
le fichier HibernateUtil.java
Le fichier FindUser.java
j'arrive pas a faire marcher hibernate, voila l'erreur ki me donne :
javax.servlet.ServletException: org.hibernate.HibernateException: No CurrentSessionContext configured! org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535) org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433) org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) cause m�re org.hibernate.HibernateException: No CurrentSessionContext configured! org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:542) com.hibernate.utils.FindUser.getInfoUser(FindUser.java:21) action.struts.LoginAction.execute(LoginAction.java:31) org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
voici ma configuration :
le fichier hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/yassine</property> <property name="hibernate.connection.username">root</property> <mapping resource="client.hbm.xml"/> </session-factory> </hibernate-configuration>
le fichier HibernateUtil.java
package com.hibernate.utils;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
/**
* Hibernate Utility class with a convenient method to get Session Factory object.
*
* @author yassinert
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Le fichier FindUser.java
package com.hibernate.utils;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Iterator;
import org.hibernate.*;
import objets.User;
/**
*
* @author yassinert
*/
public class FindUser {
public User getInfoUser(String _login, String _password)throws HibernateException{
User user =new User();
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String query = "select c.nom, c.prenom from client c where c.login='" + _login + "' and c.passe='"+ _password +"'";
Iterator results = session.createSQLQuery(query).list().iterator();
String nom=null;
String prenom=null;
while(results.hasNext())
{
Object[] row = (Object[]) results.next();
nom = (String) row[0];
prenom = (String) row[1];
}
user.setNom(nom);
user.setPrenom(prenom);
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
return user;
}
A voir également:
- Hibernate && NetBeans
- Telecharger netbeans 8.2 - Télécharger - Édition & Programmation
- Hiberfil sys disable hibernate - Guide
1 réponse
Bonjour,
Essaie de mettre la valeur de current_session_context_class à thread, voir l'exemple ci-dessous.
lin.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5433/tabase</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">postgres</property>
<!-- Nous utilisons un thread local pour la session -->
<property name="current_session_context_class">thread</property>
<property name="connection.pool_size">1</property>
<property name="show_sql">true</property>
<mapping resource="bw/entites/myobject.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Essaie de mettre la valeur de current_session_context_class à thread, voir l'exemple ci-dessous.
lin.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5433/tabase</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">postgres</property>
<!-- Nous utilisons un thread local pour la session -->
<property name="current_session_context_class">thread</property>
<property name="connection.pool_size">1</property>
<property name="show_sql">true</property>
<mapping resource="bw/entites/myobject.hbm.xml"/>
</session-factory>
</hibernate-configuration>