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 :

probleme de SessionFactory


Sujet :

Hibernate Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé Avatar de anayathefirst
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    326
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations forums :
    Inscription : Décembre 2006
    Messages : 326
    Par défaut
    salut,
    j'essay de me mettre à hibernate et je croi avoire le même problème que celui rencontré ici: http://www.developpez.net/forums/sho....php?t=127909:

    j'ai créé un classe HybernateUtil :
    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
    package util;
     
    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
                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);
        }
    }
    et j'ai fait un mapping vers objet relationnel (basique pour commancer) avec un de mes beans et la table correspondante :
    la classe :
    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
    package beans;
     
    import java.beans.PropertyChangeSupport;
    import java.beans.PropertyChangeListener;
    import java.io.Serializable;
     
    public class UserType extends Object implements Serializable {
     
        public final String PROP_DATABASE_ID="databaseId";
        public final String PROP_LIBELLE="libelle";
     
        private int databaseId;
        private String libelle;
     
        private PropertyChangeSupport propertySupport;
     
        public UserType() {
            propertySupport = new PropertyChangeSupport(this);
        }
     
        public void addPropertyChangeListener(PropertyChangeListener listener) {
            propertySupport.addPropertyChangeListener(listener);
        }
     
        public void removePropertyChangeListener(PropertyChangeListener listener) {
            propertySupport.removePropertyChangeListener(listener);
        }
     
        public int getDatabaseId() {
            return this.databaseId;
        }
     
        public void setDatabaseId(int databaseId) {
            this.databaseId = databaseId;
        }
     
        public String getLibelle() {
            return this.libelle;
        }
     
        public void setLibelle(String libelle) {
            this.libelle = libelle;
        }
     
    }
    la table :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    CREATE TABLE IF NOT EXISTS USER_TYPE
     (
       ID_USER_TYPE INTEGER NOT NULL AUTO_INCREMENT ,
       LIBELLE VARCHAR (128) NULL  
       , PRIMARY KEY (ID_USER_TYPE) 
     )
    le fichier de mapping correspondant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?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>
        <class name="beans.UserType" table="USER_TYPE">
            <id name="databaseId" type="int" column="ID_USER_TYPE" unsaved-value="0">
                <generator class="identity"/>
            </id>
            <property name="libelle" column="LIBELLE" />
        </class>
    </hibernate-mapping>
    et je teste tout ça avec une servlet tout ce qu'il y a de plus classique :
    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
     
    import java.io.*;
    import java.net.*;
     
    import javax.servlet.http.*;
     
    import util.HibernateUtil;
    import beans.User;
    import java.util.Iterator;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    public class TestServlet extends HttpServlet {
     
        /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
         * @param request servlet request
         * @param response servlet response
         */
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            Session session;
    //je n'utililise pas la transaction car de toute façon je n'arrive pas à me connecter :'(:'(
            Transaction tx;
    //c'est ici que ça foir, quand j'enlève cette ligne, ça m'affiche ce que je veut, le fait est que je ne me connecte pas :(
            session = HibernateUtil.currentSession();
     
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet TestServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet TestServlet at " 
    		+ request.getContextPath() + "</h1> by amine ;)");
     
            out.println("</body>");
            out.println("</html>");
            out.close();
    //ne sert à rien pour l'instant, mais je le garde pour la posterité :D
            HibernateUtil.closeSession();
        }
     
        // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
        /** Handles the HTTP <code>GET</code> method.
         * @param request servlet request
         * @param response servlet response
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            processRequest(request, response);
        }
     
        /** Handles the HTTP <code>POST</code> method.
         * @param request servlet request
         * @param response servlet response
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            processRequest(request, response);
        }
     
        /** Returns a short description of the servlet.
         */
        public String getServletInfo() {
            return "Short description";
        }
        // </editor-fold>
    }
    au cas où ça peut servire, voici mon fichier de configuration de hibernate :
    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
    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration
        PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
     
    <hibernate-configuration>
     
        <session-factory>
     
            <property name="connection.datasource">java:comp/env/jdbc/mariages</property>
            <property name="show_sql">false</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
     
            <!-- fichiers de mapping -->
            <!--
            <mapping resource="Adresse.hbm.xml"/>
            <mapping resource="Article.hbm.xml"/>
            <mapping resource="Couple.hbm.xml"/>
            <mapping resource="Evenement.hbm.xml"/>
            <mapping resource="FamilleArticle.hbm.xml"/>
            <mapping resource="Langue.hbm.xml"/>
            <mapping resource="Liste.hbm.xml"/>
            <mapping resource="Pays.hbm.xml"/>
            <mapping resource="Personne.hbm.xml"/>
            <mapping resource="User.hbm.xml"/>
            <mapping resource="UserType.hbm.xml"/>
            <mapping resource="Ville.hbm.xml"/>
            -->
            <mapping resource="UserType.hbm.xml"/>
     
        </session-factory>
     
    </hibernate-configuration>
    j'ai suivi un tuto sur le net à la lettre, mais je ne comprends pas pourquoi j'ai toujours les même erreurs :
    d'abord :
    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:362)

    cause m�re

    java.lang.ExceptionInInitializerError
    util.HibernateUtil.<clinit>(HibernateUtil.java:36)
    TestServlet.processRequest(TestServlet.java:39)
    TestServlet.doGet(TestServlet.java:86)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
    et quand je rafrechie la page, l'erreur change :

    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:362)

    cause m�re

    java.lang.NoClassDefFoundError
    TestServlet.processRequest(TestServlet.java:39)
    TestServlet.doGet(TestServlet.java:86)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
    je ne comprend pas ce qui arrive, aidez un jeune développeure en detresse s'il vous plai :'(,

    merci d'avance

    PS: j'utilise netbeans 5.0 comme IDE, JDK 1.4.x et hibernate 3.0

  2. #2
    Membre Expert
    Homme Profil pro
    Directeur technique
    Inscrit en
    Janvier 2007
    Messages
    1 348
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Directeur technique

    Informations forums :
    Inscription : Janvier 2007
    Messages : 1 348
    Par défaut
    Dans ta classe Util tu pourrais remplacer
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    throw new ExceptionInInitializerError(ex);
    par
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    StringWriter sw = new StringWriter();
    ex.printStackTrace(new PrintWriter(sw));
    logger.error("Stack is : "+sw.toString());
    throw new ExceptionInInitializerError(ex);
    et me dire ce que tu as dans le fichier de log ?

  3. #3
    Membre éclairé Avatar de anayathefirst
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    326
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations forums :
    Inscription : Décembre 2006
    Messages : 326
    Par défaut
    salut,
    l'erreur est :
    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
     
    Stack is : org.hibernate.InvalidMappingException: Could not parse mapping document from resource Adresse.hbm.xml
    	at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
    	at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1584)
    	at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1552)
    	at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1531)
    	at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1505)
    	at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
    	at org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
    	at util.HibernateUtil.<clinit>(HibernateUtil.java:35)
    	at tests.HibernateTest.processRequest(HibernateTest.java:48)
    	at tests.HibernateTest.doGet(HibernateTest.java:98)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
    	at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:833)
    	at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:639)
    	at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1285)
    	at java.lang.Thread.run(Unknown Source)
    Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
    	at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:508)
    	at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
    	... 23 more
    Caused by: org.dom4j.DocumentException: Error on line 8 of document  : The processing instruction target matching "[xX][mM][lL]" is not allowed. Nested exception: The processing instruction target matching "[xX][mM][lL]" is not allowed.
    	at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    	at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499)
    	... 24 more
     
    ERROR [HibernateTest]                 - "Servlet.service()" pour la servlet HibernateTest a généré une exception
    java.lang.ExceptionInInitializerError
    	at util.HibernateUtil.<clinit>(HibernateUtil.java:43)
    	at tests.HibernateTest.processRequest(HibernateTest.java:48)
    	at tests.HibernateTest.doGet(HibernateTest.java:98)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
    	at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:833)
    	at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:639)
    	at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1285)
    	at java.lang.Thread.run(Unknown Source)
    Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource Adresse.hbm.xml
    	at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
    	at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1584)
    	at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1552)
    	at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1531)
    	at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1505)
    	at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
    	at org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
    	at util.HibernateUtil.<clinit>(HibernateUtil.java:35)
    	... 16 more
    Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
    	at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:508)
    	at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
    	... 23 more
    Caused by: org.dom4j.DocumentException: Error on line 8 of document  : The processing instruction target matching "[xX][mM][lL]" is not allowed. Nested exception: The processing instruction target matching "[xX][mM][lL]" is not allowed.
    	at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    	at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499)
    	... 24 more
    j'en déduit que c'est mon fichier de mapping qui n'est pas bon

  4. #4
    Membre Expert
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 276
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 276
    Par défaut
    Est-ce que ton fichier addresse.hbm.xml est bien présent ?
    si ce n'est pas le cas, ne l'inclus pas dans la liste de tes mappings dans ton hibernate.cgf.xml.

  5. #5
    Membre éclairé Avatar de anayathefirst
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    326
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations forums :
    Inscription : Décembre 2006
    Messages : 326
    Par défaut
    salut,
    bon, le premier problème est résolue, en fait, s'était la formation des fichier xml que je créais avec netbeans :
    netbeans met toujours des commentaires au début de chaque ficher nouvellement créé pour préciser l'auteur la date et tout ça, j'ai vu dans un autre forum (celui de hibernate) que les fichier xml étaient très sencibles même au sauts de lignes (ce que j'ai trouvé bizard !!!). mais bon, le fait est que j'ai enlevé les premières ligne de commentaires et que ça a marché pour une classe (j'avais tout mis en commentaires pour tester une classe simple). et ça a marché NIKEL
    maintenant j'ai un nouveau problème, un truc de mapping, mais je jete d'abord un oeuil sur le forum et le cas échéant je fait un nouveau poste.
    je ne sais pas comment faire, mais il faut marquer que cette ligne de discussion est résolue.
    merci à tous les participants

  6. #6
    Membre éclairé Avatar de anayathefirst
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    326
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations forums :
    Inscription : Décembre 2006
    Messages : 326
    Par défaut
    Oups, je n'ai pas vu le petit (pas si petit que ça non plus) bouton « résolu »

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

Discussions similaires

  1. problem avec sessionFactory
    Par inter_amine dans le forum Hibernate
    Réponses: 1
    Dernier message: 22/01/2007, 10h03
  2. [Hibernate] probleme de SessionFactory
    Par Elmilouse dans le forum Hibernate
    Réponses: 2
    Dernier message: 11/04/2006, 16h52
  3. Probleme sur les chaines de caractere
    Par scorpiwolf dans le forum C
    Réponses: 8
    Dernier message: 06/05/2002, 19h01
  4. [Kylix] Probleme d'execution de programmes...
    Par yopziggy dans le forum EDI
    Réponses: 19
    Dernier message: 03/05/2002, 14h50
  5. [Kylix] Probleme de nombre flottant!!
    Par yopziggy dans le forum EDI
    Réponses: 5
    Dernier message: 02/05/2002, 10h13

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