J'ai voulu faire un 1er test avec hibernate, j'ai le message erreur suivant :
Avertissement: /hibernate.cfg.xml not found.
J'utilise Eclipse Indigo 1 et Hibernate 2, j'ai testé même avec Netbeans 7 pareil et aussi avec Hibernate 3. J'ai testé aussi de préciser le chemin du dossier dans SessionFactory comme ça :
SessionFactory sessionFactory = config.configure("hibernate.cfg.xml").buildSessionFactory();
ou
SessionFactory sessionFactory = config.configure("file:///H:/hibernate.cfg.xml").buildSessionFactory();
J'ai mis le fichier hibernate.cfg.xml partout pareil. Si quelqu'un peut m'aider je galère depuis plusieurs jours. Voilà tous le message sur Eclipse.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| mars 02, 2012 2:42:15 AM net.sf.hibernate.cfg.Environment <clinit>
Infos: Hibernate 2.1.2
mars 02, 2012 2:42:15 AM net.sf.hibernate.cfg.Environment <clinit>
Infos: hibernate.properties not found
mars 02, 2012 2:42:15 AM net.sf.hibernate.cfg.Environment <clinit>
Infos: using CGLIB reflection optimizer
mars 02, 2012 2:42:15 AM net.sf.hibernate.cfg.Configuration configure
Infos: configuring from resource: /hibernate.cfg.xml
mars 02, 2012 2:42:15 AM net.sf.hibernate.cfg.Configuration getConfigurationInputStream
Infos: Configuration resource: /hibernate.cfg.xml
mars 02, 2012 2:42:15 AM net.sf.hibernate.cfg.Configuration getConfigurationInputStream
Avertissement: /hibernate.cfg.xml not found
Exception in thread "main" net.sf.hibernate.HibernateException: /hibernate.cfg.xml not found
at net.sf.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:831)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:855)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:842)
at TestHibernate.main(TestHibernate.java:10) |
> Description du projet
nom projet java=hibernate-test
base de donne mysql=donnee
table=users avec 3 colonne:
id int(11)
nom varchar (50)
prenom varchar (50)
Le fichier hibernate.cfg.xml et User.hbm.xml se trouve dans le classe-path. Les librairies Hibernate et driver de la bdd se trouve dans le dossier lib. La liste des librairies est dans le fichier .classpath ci-dessous.
> .classpath
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="classe"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/cglib-full-2.0.2.jar"/>
<classpathentry kind="lib" path="lib/commons-collections-2.1.1.jar"/>
<classpathentry kind="lib" path="lib/commons-logging-1.0.4.jar"/>
<classpathentry kind="lib" path="lib/dom4j-1.4.jar"/>
<classpathentry kind="lib" path="lib/ehcache-0.9.jar"/>
<classpathentry kind="lib" path="lib/hibernate2.jar"/>
<classpathentry kind="lib" path="lib/jta.jar"/>
<classpathentry kind="lib" path="lib/jtds-0.8.1.jar"/>
<classpathentry kind="lib" path="lib/mysql-connector-java-5.1.18-bin.jar"/>
<classpathentry kind="lib" path="lib/odmg-3.0.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath> |
> fichier hibernate.cfg.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/donnee</property>
<property name="hibernate.connection.username">myusername</property>
<property name="hibernate.connection.password">mypassword</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="User.hbm.xml"/>
</session-factory>
</hibernate-configuration> |
> fichier mapping: User.hbm.xml
1 2 3 4 5 6 7 8 9 10 11 12
| <?xml version="1.0"?><!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="User" table="users">
<id name="id" type="int" column="id">
<generator class="identity"/>
</id>
<property name="nom" type="string" not-null="true" />
<property name="prenom" type="string" not-null="true" />
</class>
</hibernate-mapping> |
> classe bean:User.java
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
| public class User {
private int id;
private String nom;
private String prenom;
public User(String nomPersonne, String prenomPersonne) {
this.nom = nomPersonne;
this.prenom= prenomPersonne;
}
public User() {
}
public int getId() {
return id;
}
public String getNom() {
return nom;
}
public String getPrenom() {
return prenom;
}
public void setId(int id) {
this.id = id;
}
public void setNomPersonne(String nomPersonne) {
this.nom = nomPersonne;
}
public void setPrenomPersonne(String prenomPersonne) {
this.prenom = prenomPersonne;
}
} |
> classe principale :TestHibernate.java
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
| import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import java.util.*;
public class TestHibernate {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
Configuration config = new Configuration();
config.addClass(User.class);
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
User personne = new User("nom3", "prenom3");
session.save(personne);
session.flush() ;
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
sessionFactory.close();
}
} |
Partager