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 :

pbl failed.org.hibernate.MappingException: entity class not found: User


Sujet :

Hibernate Java

  1. #1
    Membre averti Avatar de Philcmoi
    Homme Profil pro
    Inscrit en
    Juillet 2006
    Messages
    666
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juillet 2006
    Messages : 666
    Points : 412
    Points
    412
    Par défaut pbl failed.org.hibernate.MappingException: entity class not found: User
    la classe User et User.hbm.xml sont pourtant dans le meme package.
    User.hbm.xml:
    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
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- Generated 10 juin 2010 12:39:25 by Hibernate Tools 3.2.4.GA -->
    <hibernate-mapping>
        <class name="User" table="user" catalog="mabasse">
            <id name="idUser" type="java.lang.Integer">
                <column name="id_user" />
                <generator class="increment" />
            </id>
            <property name="loginUser" type="string">
                <column name="login_user" length="25" not-null="true" />
            </property>
            <property name="passwordUser" type="string">
                <column name="password_user" length="25" not-null="true" />
            </property>
        </class>
    </hibernate-mapping>

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Décembre 2003
    Messages
    476
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2003
    Messages : 476
    Points : 595
    Points
    595
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
      <class name="User" ...
    Meme si le mapping et la classe java sont dans la même arbo, je pense que tu n'y échapperas pas : tu dois mettre le nom qualifiée de la classe (package + nom simple de la classe)

  3. #3
    Membre averti Avatar de Philcmoi
    Homme Profil pro
    Inscrit en
    Juillet 2006
    Messages
    666
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juillet 2006
    Messages : 666
    Points : 412
    Points
    412
    Par défaut
    Merci

  4. #4
    Membre averti Avatar de Philcmoi
    Homme Profil pro
    Inscrit en
    Juillet 2006
    Messages
    666
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juillet 2006
    Messages : 666
    Points : 412
    Points
    412
    Par défaut pbl Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured
    classe User
    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
    package com.map;
    import org.hibernate.*;
     
    import com.util.HibernateUtil;
    import com.util.*;
    public class testInsert {
     
    	 public static void main(String[] args)
    		throws HibernateException {
     
    		 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    		 session.beginTransaction();
     
    		 User user = new User();
    		 user.setLoginUser("wfh");
    		 user.setPasswordUser("wfh");
    		 session.save(user);
     
    		 session.getTransaction().commit();
    		 HibernateUtil.getSessionFactory().close();
    	 }
    }
    User.hbm.xml
    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
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- Generated 10 juin 2010 12:39:25 by Hibernate Tools 3.2.4.GA -->
    <hibernate-mapping>
        <class name="com.map.User" table="user" catalog="mabasse">
            <id name="idUser" type="java.lang.Integer">
                <column name="id_user" />
                <generator class="increment" />
            </id>
            <property name="loginUser" type="string">
                <column name="login_user" length="25" not-null="true" />
            </property>
            <property name="passwordUser" type="string">
                <column name="password_user" length="25" not-null="true" />
            </property>
        </class>
    </hibernate-mapping>
    hibernate.config.xml
    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
    <?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.bytecode.use_reflection_optimizer">false</property>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.password">123</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mabasse</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            <mapping resource="com/map/User.hbm.xml" />
     
        </session-factory>
    </hibernate-configuration>
    hibernate util
    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
    package com.util;
     
    import org.hibernate.*;
    import org.hibernate.cfg.*;
     
    public class HibernateUtil {
    	public static final SessionFactory sessionFactory;
     
    	static {
    		try {
    			// Création de la SessionFactory à partir de hibernate.cfg.xml
    			sessionFactory = new Configuration().configure().buildSessionFactory();
    		} catch (Throwable ex) {
    			// Make sure you log the exception, as it might be swallowed
    			System.err.println("Initial SessionFactory creation failed." + ex);
    			throw new ExceptionInInitializerError(ex);
    		}
    	}
     
    	public static final ThreadLocal session = new ThreadLocal();
     
    	public static SessionFactory getSessionFactory() {
    		return sessionFactory;
    	}
    }
    Lorsque j'execute ce main
    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
    package com.map;
    import org.hibernate.*;
     
    import com.util.HibernateUtil;
    import com.util.*;
    public class testInsert {
     
    	 public static void main(String[] args)
    		throws HibernateException {
     
    		 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    		 session.beginTransaction();
     
    		 User user = new User();
    		 user.setLoginUser("wfh");
    		 user.setPasswordUser("wfh");
    		 session.save(user);
     
    		 session.getTransaction().commit();
    		 HibernateUtil.getSessionFactory().close();
    	 }
    }
    j'ai l'erreure
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
    	at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:696)
    	at com.map.testInsert.main(testInsert.java:11)
    Désolais pour vous soliciter autant je suis nouveau en hibernate

  5. #5
    Membre confirmé
    Profil pro
    Inscrit en
    Décembre 2003
    Messages
    476
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2003
    Messages : 476
    Points : 595
    Points
    595
    Par défaut
    Pas de souci

    Comme indiqué dans le message d'erreur, la configuration de ta sessionFactory n'as pas de CurrentSessionContext configuré.
    Or pour utiliser la méthode getCurrentSessionContext(), tu as besoin de choisir une implémentation de l'interface org.hibernate.context.CurrentSessionContext.

    Ajoute la propriété
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <property name="current_session_context_class">thread</property>
    dans la déclaration de ta session-factory.

    Tu pourrais avoir d'autres valeur mais au vu de ta configuration Hibernate, tu n'utilises pas Jta ou d'autres joyeusetés.

    Dans ta classe HibernateUtil, ce morceau de code ne sert pas :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    public static final ThreadLocal session = new ThreadLocal();
    Bon courage

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

Discussions similaires

  1. Réponses: 6
    Dernier message: 06/10/2009, 11h30
  2. Réponses: 3
    Dernier message: 15/04/2009, 21h08
  3. Réponses: 8
    Dernier message: 17/04/2008, 17h29
  4. mapping et entity class not found
    Par mauroyb0 dans le forum Hibernate
    Réponses: 8
    Dernier message: 26/03/2007, 14h39
  5. [HIBERNATE] Conf et classe not found
    Par djodjo dans le forum Hibernate
    Réponses: 2
    Dernier message: 10/05/2006, 16h37

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