Ajouter un commentaire

icha_7146
proléme 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

<?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

<?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

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

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

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

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

### 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

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

Filtered HTML

Plain text

CAPTCHA
Cette question permet de vérifier que vous n'êtes pas un robot spammeur :-)
  GGG    QQQ    X   X  X   X  X   X 
G Q Q X X X X X X
G GG Q Q X X X
G G Q QQ X X X X X X
GGG QQQQ X X X X X X
Q