Je decouvre Hibernate et la je suis confronté a un probleme que je ne comprend pas de meme que mon entourage. j'ai suivi un tuto :
http://www.laliluna.de/download/firs...utorial-en.pdf

Mais ca ne marche pas du tout.

Mon objectif et une premier prise en mais, soit par la simple verification du login et mot de passe en verifiant sur une base de donnée "test" et sur la table "user"

hibernate.cfg.xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
 
<?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="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/test</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <mapping resource="socgen.portail.hibernate/user.hbm.xml"/>
  </session-factory>
</hibernate-configuration>
user.hbm.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="de.laliluna.example">
  <class name="User" table="user">
    <id column="name" name="name" type="java.lang.String">
    </id>
    <property column="mdp" name="mdp" type="java.lang.String"/>
  </class>
</hibernate-mapping>
UtilisateurMapping
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
 
package socgen.portail.hibernate.mapping;
 
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import socgen.portail.form.ConnexionForm;
import socgen.portail.hibernate.HibernateSessionFactory;
import socgen.portail.sdf.HibernateUtil;
/**
 * @author laliluna
 *
 */
public class UtilisateurMapping {
 
    public static ConnexionForm selectUser(String login) {
        Session session = null;
        Transaction tx = null;
        Logger log = Logger.getLogger("Utilisateur");
        log.info("Search user");
        ConnexionForm user = null;
        try {
// [laliluna] get the session from the factory
            //session = HibernateSessionFactory.currentSession();
            session = (Session) HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
// [laliluna] always start a transaction before doing something
// (even reading) from the database
 
            user = (ConnexionForm) session.find("select from User where login ="+ login);
 
// [laliluna] commit your transaction or nothing is wrote to the db
            tx.commit();
// [laliluna] clean up (close the session)
            session.close();
            return user;
 
        } catch (HibernateException e) {
// [laliluna] when an error occured, try to rollback your
// transaction
            if (tx != null) {
                try {
                    tx.rollback();
                } catch (HibernateException e1) {
                    log.warn("rollback not successful");
                }
            }
            /*
             * [laliluna] close your session after an exception!! Your session
             * is in an undefined and unstable situation so throw it away!
             *
             */
            if (session != null) {
                try {
                    session.close();
                } catch (HibernateException e2) {
                    log.warn("session close not successful");
                }
            }
        }
        return user;
    }
}
J'ai dabord utiliser la fonction de connection avec la database du tuto mais cela ne marché pas
HibernateSessionFactory
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
 
package socgen.portail.hibernate;
 
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.cfg.Configuration;
 
/**
 * Configures and provides access to Hibernate sessions, tied to the
 * current thread of execution. Follows the Thread Local Session
 * pattern, see {@link http://hibernate.org/42.html}.
 */
public class HibernateSessionFactory {
 
    /**
     * Location of hibernate.cfg.xml file.
     * NOTICE: Location should be on the classpath as Hibernate uses
     * #resourceAsStream style lookup for its configuration file. That
     * is place the config file in a Java package - the default location
     * is the default Java package.<br><br>
     * Examples: <br>
     * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
     * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
     */
    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
    /** Holds a single instance of Session */
    private static final ThreadLocal threadLocal = new ThreadLocal();
    /** The single instance of hibernate configuration */
    private static final Configuration cfg = new Configuration();
    /** The single instance of hibernate SessionFactory */
    private static net.sf.hibernate.SessionFactory sessionFactory;
 
    /**
     * Returns the ThreadLocal Session instance. Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     * @return Session
     * @throws HibernateException
     */
    public static Session currentSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        if (session == null || !session.isConnected()) {
            if (sessionFactory == null) {
                try {
                    cfg.configure(CONFIG_FILE_LOCATION);
                    sessionFactory = cfg.buildSessionFactory();
                } catch (Exception e) {
                    System.err.println("%%%% Error Creating SessionFactory %%%%");
                    e.printStackTrace();
                }
            }
            session = sessionFactory.openSession();
            threadLocal.set(session);
        }
        return session;
    }
 
    /**
     * Close the single hibernate session instance.
     *
     * @throws HibernateException
     */
    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);
        if (session != null) {
            session.close();
        }
    }
 
    /**
     * Default constructor.
     */
    private HibernateSessionFactory() {
    }
}
Un collegue m'a fourni cette fonction qui l'utilise mais elle n'a pas marche non plus

HibernateSessionFactory
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
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package socgen.portail.sdf;
 
 
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
 
/**
 * Hibernate Utility class with a convenient method to get Session Factory object.
 *
 * @author A329980
 */
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;
    }
}
Je pense que l'erreur doit venir du hibernate.cfg.xml ou du user.hbm.xml

Rappel j'utilise Struts 1.2.9 donc ma classe est (je ne pense pas que la fonction validate change le tout):

ConnexionForm.java
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
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package socgen.portail.form;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
 
/**
 *
 * @author A329820
 */
public class ConnexionForm extends org.apache.struts.action.ActionForm {
 
    private String login;
 
    public String getLogin() {
        return login;
    }
 
    public void setLogin(String login) {
        this.login = login;
    }
 
    private String mdp;
 
    public String getMdp() {
        return mdp;
    }
 
    public void setMdp(String mdp) {
        this.mdp = mdp;
    }
 
 
 
    /**
     *
     */
    public ConnexionForm() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        String login = getLogin();
        String mdp = getMdp();
        if (login == null || login.length() < 1) {
            errors.add("login", new ActionMessage("error.invalid"));
            // TODO: add 'error.name.required' key to your resources
        }
        if (mdp == null || mdp.length() < 1) {
            errors.add("login", new ActionMessage("error.invalid"));
            // TODO: add 'error.name.required' key to your resources
        }
        String error = errors.toString();
        return errors;
    }
}