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 :

Probléme avec hibernate


Sujet :

Hibernate Java

  1. #1
    Nouveau candidat au Club
    Inscrit en
    Mai 2009
    Messages
    1
    Détails du profil
    Informations forums :
    Inscription : Mai 2009
    Messages : 1
    Par défaut Probléme avec hibernate
    Bonsoir

    Je suis débutante en Hiebrnate et je trouve des difficulters j'espére que vous allez m'aide parceque vraiment je me bloque là

    bon voila j'ai crée dans une premier temps une projet sous eclipse puis j'ai ejouter les libraires de hibernate + le driver de postgresql
    aprés j'ai crée un fichier de configuration hibernate.cfg.xml voilà son contenu

    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
    <?xml version="1.0" encoding="utf-8"?>
     <! DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "- / / Hibernate / Hibernate Mapping DTD 3.0 / / EN" 
        " http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
     
     
    <hibernate-configuration>
    	<session-factory>
    		<!-- local connection properties -->
    		<property name="hibernate.connection.url">
    			jdbc:postgresql://localhost:5432/DBCRJJ
    		</property>
    		<property name="hibernate.connection.driver_class">
    			org.postgresql.Driver
    		</property>
    		<property name="hibernate.connection.username">postgres</property>
    		<property name="hibernate.connection.password">1234</property>
    		<!-- property name="hibernate.connection.pool_size"></property -->
    		<!-- dialect for PostgreSQL -->
    		<property name="dialect">
    			org.dialect.PostgreSQLDialect
    		</property>
    		<property name="hibernate.show_sql">false</property>
    		<property name="hibernate.use_outer_join">true</property>
    		<!--
    			<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
    			<property name="jta.UserTransaction">java:comp/UserTransaction</property>
    		//-->
    		<property name="hibernate.transaction.factory_class">
    			org.hibernate.transaction.JDBCTransactionFactory
    		</property>
    		<mapping resource="contact.hbm" />
    	</session-factory>
    </hibernate-configuration>
    aprés j'ai crée e fichier de mapping voilà son contenu

    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
    <?xml version="1.0"?>
     <! DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "- / / Hibernate / Hibernate Mapping DTD 3.0 / / EN" 
        " http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
     
     
    <hibernate-mapping package="PkCOntact">
    	<class name="PKCOntact.contact" table="contact">
    		<id
    			column="id"
    			name="id"
    			type="integer"
    		>
    			<generator class="increment" />
    		</id>
    		<property
    			column="prenom"
    			length="25"
    			name="prenom"
    			not-null="false"
    			type="string"
    		 />
    		<property
    			column="age"
    			length="10"
    			name="age"
    			not-null="false"
    			type="integer"
    		 />
    		<property
    			column="nom"
    			length="25"
    			name="nom"
    			not-null="false"
    			type="string"
    		 />
    	</class>
    </hibernate-mapping>
    j'ai crée une class bean voilà son contenu

    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
    package PkCOntact;
     
    public class contact {
    	private int id;
    	private String nom;
    	private String prenom;
    	private int age;
     
    	public contact() {
     
    	}
     
    	public contact(int id, String nom, String prenom, int age) {
     
    		this.id = id;
    		this.nom = nom;
    		this.prenom = prenom;
    		this.age = age;
    	}
     
    	public int getId() {
    		return id;
    	}
     
    	public void setId(int id) {
    		this.id = id;
    	}
     
    	public String getNom() {
    		return nom;
    	}
     
    	public void setNom(String nom) {
    		this.nom = nom;
    	}
     
    	public String getPrenom() {
    		return prenom;
    	}
     
    	public void setPrenom(String prenom) {
    		this.prenom = prenom;
    	}
     
    	public int getAge() {
    		return age;
    	}
     
    	public void setAge(int age) {
    		this.age = age;
    	}
     
     
     
     
    }
    puis le class qui contient ma sessionfactory sous le nom de HibernateUtil.java voilà son contenu

    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
    package PkCOntact;
    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.cfg.Configuration;
     
     
    public class HibernateUtil {
    	private static String CONFIG_FILE_LOCATION="/hibernate.cfg.xml";
    	private static final ThreadLocal<Session> threadlocal=new ThreadLocal<Session>();
    	private static final Configuration cfg =new Configuration();
    	private static org.hibernate.SessionFactory sessionFactory;
    	public static Session currentSession()throws HibernateException{
    	Session session=(Session)threadlocal.get();	
    	if(session==null||!session.isOpen()){
    		if(sessionFactory==null){
    			try{
    				cfg.configure(CONFIG_FILE_LOCATION);
    				sessionFactory=cfg.buildSessionFactory();
     
    			}
    			catch(Exception ex){
    				System.err.println("Erreur");
    				ex.printStackTrace();
    			}}
    			session=(sessionFactory!=null)?sessionFactory.openSession():null;
    			threadlocal.set(session);}
    			return session;
     
    		}
    		public static void CloseSession()throws HibernateException{
    			Session session=(Session)threadlocal.get();
    			threadlocal.set(null);
    			if(session!=null){
    				session.close();
    			}
    		}
    }
    aprés une class man MainRead.java voilà son contenu
    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
    package PkCOntact;
     
    import java.util.Iterator;
     
    import org.hibernate.Session;
     
    public class MainRead {
    public static void main(String []args){
     
    				Session session=HibernateUtil.currentSession();
    				Iterator iter=session.createQuery("from contact").iterate();
    				System.out.println("| id || nom || prenom || Age |");
    				while(iter.hasNext()){
    					contact contact=(contact)iter.next();
    				System.out.println("|"+contact.getId());
    				System.out.println("  "+contact.getNom()+" ");
    				System.out.println(contact.getPrenom()+"  ");
    				System.out.println(contact.getAge()+" |");
    				System.out.println();
    		             }
     
    				HibernateUtil.CloseSession();
     
     
     
     
     
    }
    }

    j'ai executé j'ai reçu cette 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
    log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
    log4j:WARN Please initialize the log4j system properly.
    Erreur
    org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
    	at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1500)
    	at org.hibernate.cfg.Configuration.configure(Configuration.java:1434)
    	at PkCOntact.HibernateUtil.currentSession(HibernateUtil.java:17)
    	at PkCOntact.MainRead.main(MainRead.java:10)
    Caused by: org.dom4j.DocumentException: Error on line 2 of document  : The markup in the document preceding the root element must be well-formed. Nested exception: The markup in the document preceding the root element must be well-formed.
    	at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    	at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1490)
    	... 3 more
    Exception in thread "main" java.lang.NullPointerException
    	at PkCOntact.MainRead.main(MainRead.java:11)
    j'ai fait une recherche sur l'erreur "log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
    log4j:WARN Please initialize the log4j system properly." alors j'ai constaté qu'il faut que j' ajoute un fichier log4j.properties au src de mon projet avec se contenu

    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
    ### direct log messages to stdout ###
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    ### set log levels - for more verbose logging change 'info' to 'debug' ###
    log4j.rootLogger=debug, stdout
    log4j.logger.org.hibernate=info
    #log4j.logger.org.hibernate=debug
    ### log HQL query parser activity
    #log4j.logger.org.hibernate.hql.ast.AST=debug
    ### log just the SQL
    log4j.logger.org.hibernate.SQL=debug
    ### log JDBC bind parameters ###
    log4j.logger.org.hibernate.type=info
    ### log schema export/update ###
    log4j.logger.org.hibernate.tool.hbm2ddl=info
    ### log HQL parse trees
    #log4j.logger.org.hibernate.hql=debug
     
    ### log cache activity ###
    log4j.logger.org.hibernate.cache=info
    ### log transaction activity
    #log4j.logger.org.hibernate.transaction=debug
    ### log JDBC resource acquisition
    #log4j.logger.org.hibernate.jdbc=debug
    ### enable the following line if you want to track down connection ###
    ### leakages when using DriverManagerConnectionProvider ###
    #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
    aprés que j'ai ajouté ce fichier log je ne recois plus l'erreur concernant le log

    je recois ces 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
    21:37:41,218  INFO Environment:514 - Hibernate 3.2.6
    21:37:41,234  INFO Environment:547 - hibernate.properties not found
    21:37:41,234  INFO Environment:681 - Bytecode provider name : cglib
    21:37:41,234  INFO Environment:598 - using JDK 1.4 java.sql.Timestamp handling
    21:37:41,328  INFO Configuration:1432 - configuring from resource: /hibernate.cfg.xml
    21:37:41,328  INFO Configuration:1409 - Configuration resource: /hibernate.cfg.xml
    21:37:41,546 ERROR XMLHelper:61 - Error parsing XML: /hibernate.cfg.xml(2) The markup in the document preceding the root element must be well-formed.
    Erreur
    org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
    	at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1500)
    	at org.hibernate.cfg.Configuration.configure(Configuration.java:1434)
    	at PkCOntact.HibernateUtil.currentSession(HibernateUtil.java:17)
    	at PkCOntact.MainRead.main(MainRead.java:10)
    Caused by: org.dom4j.DocumentException: Error on line 2 of document  : The markup in the document preceding the root element must be well-formed. Nested exception: The markup in the document preceding the root element must be well-formed.
    	at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    	at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1490)
    	... 3 more
    Exception in thread "main" java.lang.NullPointerException
    	at PkCOntact.MainRead.main(MainRead.java:11)
    alors la je me bloque et j'attend votre aide

    Merci d'avance

  2. #2
    Membre éclairé Avatar de aelmalki
    Inscrit en
    Mars 2009
    Messages
    250
    Détails du profil
    Informations forums :
    Inscription : Mars 2009
    Messages : 250
    Par défaut
    t'as mis où ton fichier de configuration ?

    Je pense que cette erreur est générer parce qu'il ne trouve pas le fichier de configuration.

    Si ce n'est pas le cas, j'aimerai bien savoir la source de l'erreur

  3. #3
    Membre éclairé Avatar de aelmalki
    Inscrit en
    Mars 2009
    Messages
    250
    Détails du profil
    Informations forums :
    Inscription : Mars 2009
    Messages : 250
    Par défaut
    Je pense que dans la DTD il y'a une ligne en double :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     <! DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "- / / Hibernate / Hibernate Mapping DTD 3.0 / / EN" 
        " http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    est ce que tu peux rectifier et rester ?

Discussions similaires

  1. Problème avec hibernate et MySQL!
    Par sofien dans le forum Outils
    Réponses: 5
    Dernier message: 27/08/2008, 09h06
  2. Les problémes avec Hibernate
    Par hichem_enis dans le forum Struts 1
    Réponses: 7
    Dernier message: 11/04/2008, 13h00
  3. Problème avec Hibernate
    Par ISID dans le forum Hibernate
    Réponses: 13
    Dernier message: 05/10/2007, 12h27
  4. Problème avec Hibernate synchronizer
    Par jason69 dans le forum Hibernate
    Réponses: 2
    Dernier message: 27/08/2007, 11h35
  5. [Hibernate] Problème avec Hibernate et Eclipse 3
    Par theseuby dans le forum Eclipse Java
    Réponses: 1
    Dernier message: 30/03/2006, 21h31

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