IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Hibernate Java Discussion :

[Debutant] [struts] Probleme avec hibernate


Sujet :

Hibernate Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    89
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2009
    Messages : 89
    Par défaut [Debutant] [struts] Probleme avec hibernate
    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;
        }
    }

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    89
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2009
    Messages : 89
    Par défaut
    Je suis peut etre trop vague...
    Dans le pire des cas pourriez vous me réorianté vers un tuto struts utilisant hibernate sur netbean 6.5?

    Merci

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    89
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2009
    Messages : 89
    Par défaut
    Voici ma page d'erreur

    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
     
    type Rapport d'exception
     
    message
     
    description Le serveur a rencontr� une erreur interne () qui l'a emp�ch� de satisfaire la requ�te.
     
    exception
     
    javax.servlet.ServletException: L'ex�cution de la servlet a lanc� une exception
    	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
     
    cause m�re
     
    java.lang.ExceptionInInitializerError
    	socgen.portail.hibernate.mapping.UtilisateurMapping.selectUser(UtilisateurMapping.java:126)
    	socgen.portail.action.ConnexionAction.execute(ConnexionAction.java:51)
    	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)
    	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
     
    cause m�re
     
    java.lang.RuntimeException: Problème de configuration : problem parsing configuration/hibernate.cfg.xml
    	socgen.portail.sdf.HibernateUtil.<clinit>(HibernateUtil.java:17)
    	socgen.portail.hibernate.mapping.UtilisateurMapping.selectUser(UtilisateurMapping.java:126)
    	socgen.portail.action.ConnexionAction.execute(ConnexionAction.java:51)
    	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)
    	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
     
    cause m�re
     
    net.sf.hibernate.HibernateException: problem parsing configuration/hibernate.cfg.xml
    	net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:972)
    	net.sf.hibernate.cfg.Configuration.configure(Configuration.java:911)
    	net.sf.hibernate.cfg.Configuration.configure(Configuration.java:897)
    	socgen.portail.sdf.HibernateUtil.<clinit>(HibernateUtil.java:14)
    	socgen.portail.hibernate.mapping.UtilisateurMapping.selectUser(UtilisateurMapping.java:126)
    	socgen.portail.action.ConnexionAction.execute(ConnexionAction.java:51)
    	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)
    	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
     
    cause m�re
     
    org.dom4j.DocumentException: Error on line 1 of document http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd : The markup declarations contained or pointed to by the document type declaration must be well-formed. Nested exception: The markup declarations contained or pointed to by the document type declaration must be well-formed.
    	org.dom4j.io.SAXReader.read(SAXReader.java:482)
    	net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:967)
    	net.sf.hibernate.cfg.Configuration.configure(Configuration.java:911)
    	net.sf.hibernate.cfg.Configuration.configure(Configuration.java:897)
    	socgen.portail.sdf.HibernateUtil.<clinit>(HibernateUtil.java:14)
    	socgen.portail.hibernate.mapping.UtilisateurMapping.selectUser(UtilisateurMapping.java:126)
    	socgen.portail.action.ConnexionAction.execute(ConnexionAction.java:51)
    	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)
    	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
    J'ai changé encor de HibernateUtil pour prendre celui du tuto de developpez.com

    http://defaut.developpez.com/tutorie...pse/hibernate/

    Voici le code HibernateUtil

    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
     
    package com.minosis.hibernate;
     
    import net.sf.hibernate.*;
    import net.sf.hibernate.cfg.*;
     
    public class HibernateUtil {
     
     private static final SessionFactory sessionFactory;
     
     static {
       try {
       // Crée la SessionFactory
       sessionFactory =
    	 new Configuration().configure().buildSessionFactory();
       } catch (HibernateException ex) {
       throw new RuntimeException("Problème de configuration : "
       + ex.getMessage(), ex);
       }
       }
     
     public static final ThreadLocal session = new ThreadLocal();
     
     public static Session currentSession()
    		throws HibernateException {
       Session s = (Session) session.get();
       // Ouvre une nouvelle Session, si ce Thread n'en a aucune
       if (s == null) {
       s = sessionFactory.openSession();
       session.set(s);
       }
       return s;
       }
     
     public static void closeSession()
    		throws HibernateException {
       Session s = (Session) session.get();
       session.set(null);
       if (s != null)
       s.close();
       }
     }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [debutant][swing] Probleme avec un Jpanel
    Par JeanMoul dans le forum Débuter
    Réponses: 6
    Dernier message: 27/08/2005, 19h07
  2. [Struts] Problème avec la méthode validate
    Par clement42 dans le forum Struts 1
    Réponses: 2
    Dernier message: 09/06/2005, 10h52
  3. Problèmes avec Hibernate (sous Eclipse)
    Par Pierric dans le forum Hibernate
    Réponses: 2
    Dernier message: 07/04/2005, 14h35
  4. [STRUTS] Probleme avec le tiles Framework
    Par SEMPERE Benjamin dans le forum Struts 1
    Réponses: 7
    Dernier message: 02/10/2004, 14h11
  5. [Debutant(e)]probleme avec un replaceAll
    Par Jovial dans le forum Langage
    Réponses: 11
    Dernier message: 14/06/2004, 16h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo