Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3
Salut : Salam 3alikom
je suis un debutant pour hibernate, je trouve beaucoup de problemme et je suis besoin d'aide:
mon premier problemme est que l'or de l'execution hibernate lance une Exception qu'il n'a pas trouvé le fichier configuration et le fichier de mapping de base de donne
Personne.hbm.xml:
voici le message :
hibernate.cfg.xml not found
donc je fait cette solution est je presise moi me l'emplacement du fichier il marche bien.
Solution :
Quote:
sessionFactory =
new Configuration().configure(new File("./hibernate.cfg.xml")).addFile(new File("./Personne.hbm.xml")).buildSessionFactory();
2 éme problemme:une Exception de mapping de fichier hbm.xml se lance voici le detail :
Exception :Quote:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.ExceptionInInitializerError
at hibernate.Test.main(Test.java:14)
Caused by: java.lang.RuntimeException: Problème de configuration : Could not parse mapping document from file .\Personne.hbm.xml
at hibernate.HibernateUtil.<clinit>(HibernateUtil.java:22)
... 1 more
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from file .\Personne.hbm.xml
at org.hibernate.cfg.Configuration.addFile(Configuration.java:325)
at hibernate.HibernateUtil.<clinit>(HibernateUtil.java:19)
... 1 more
Caused by: org.dom4j.DocumentException: hibernate.sourceforge.net Nested exception: hibernate.sourceforge.net
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.dom4j.io.SAXReader.read(SAXReader.java:264)
at org.hibernate.cfg.Configuration.addFile(Configuration.java:311)
... 2 more
Java Result: 1BUILD SUCCESSFUL (total time: 10 seconds)
Personne.hbm.xml :Quote:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="exemples.Personne" table="Personne">
<id name="Id" type="integer">
<column name="id" sql-type="int(4)"/>
<generator class="increment" />
</id>
<property name="Nom" type="string">
<column name="nom" sql-type="char(50)" not-null="true"/>
</property>
<property name="Prenom" type="string">
<column name="prenom" sql-type="char(50)" not-null="true"/>
</property>
</class>
</hibernate-mapping>
hibernate.cfg.xml :Quote:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration (View Source for full doctype...)>
- <hibernate-configuration>
- <session-factory>
- <!-- local connection properties
-->
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://127.0.0.1:1433/Base1</property>
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.username">user1</property>
<property name="hibernate.connection.password">password</property>
- <!-- property name="hibernate.connection.pool_size"></property
-->
- <!-- dialect for Microsoft SQL Server
-->
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.use_outer_join">true</property>
- <!--
<property name="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFactory</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
//
-->
<property name="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JDBCTransactionFactory</property>
<mapping resource="TContact.hbm" />
</session-factory>
</hibernate-configuration>
Personne.java :Quote:
public class Personne {
private Integer Id;
private String Nom;
private String Prenom;
public Personne(){
}
public void setId(Integer id){
this.Id=id;
}
public void setNom(String nom){
this.Nom=nom;
}
public void setPrenom(String prenom){
this.Prenom=prenom;
}
public Integer getId(){
return this.Id;
}
public String getNom(){
return this.Nom;
}
public String getPrenom(){
return this.Prenom;
}
}
HibernateUtil :Quote:
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Crée la SessionFactory
sessionFactory =
new Configuration().configure(new File("./hibernate.cfg.xml")).addFile(new File("./Personne.hbm.xml")).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();
}
}
Class de test :Quote:
import java.util.*;
import org.hibernate.*;
public class Test {
public static void main(String[] args)
throws HibernateException {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Personne contact = new Personne();
contact.setNom("Dupont");
contact.setPrenom("Jean");
session.save(contact);
contact = new Personne();
contact.setNom("Lambert");
contact.setPrenom("Julie");
session.save(contact);
tx.commit();
HibernateUtil.closeSession();
}
}