Salut tout le monde, je suis une débutante de l'utilisation d'hibernate j'ai essayée de faire un petit exemple mais j'ai cette exception:
Hello, I am a beginner of using hibernate I tried to make a small example but I have this exception:
Initial SessionFactory creation failed.org.hibernate.MappingException: invalid configuration
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.mql.util.HibernateUtil.<clinit>(HibernateUtil.java:18)
at com.mql.test.TesTHibernate.main(TesTHibernate.java:11)
Caused by: org.hibernate.MappingException: invalid configuration
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1347)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1322)
at com.mql.util.HibernateUtil.<clinit>(HibernateUtil.java:13)
... 1 more
Caused by: org.xml.sax.SAXParseException: Document is invalid: no grammar found.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1344)voilà mes fihciers:
j'ai une classe java bean User
here are my files:
I have a class
Code:
package com.mql.vo;
/**
* @author KhadijaRSS
*
*/
public class User {
private int idUser;
private String login;
private String pass;
public User() {
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public void setIdUser(int idUser) {
this.idUser = idUser;
}
public int getIdUser() {
return idUser;
}
}
et le ficher de mapping associé:
and the associated mapping file:
Code:
<hibernate-mapping package="com.mql.vo">
<class name="User" table="Compte">
<id name="idUser" column="idCompte">
<generator class="native"/>
</id>
<property name="login" column="login"/>
<property name="pass" column="password"/>
</class>
</hibernate-mapping>
une classe de creation d'une session hibernate:
a class to create a Hibernate session:
Code:
package com.mql.util;
import java.io.File;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
public static final SessionFactory sessionFactory;
static {
try {
// Création de la SessionFactory à partir de hibernate.cfg.xml
File f=new File("D:/JAVA/workspace/Test1Hibernate/WebContent/WEB-INF/classes/com/mql/util/hibernate.cfg.xml");
sessionFactory = new Configuration().configure(f).buildSessionFactory();
sessionFactory.openSession();
} 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 SessionFactory getSessionFactory() {
return sessionFactory;
}
}
le fichier de configuration:
configuration file:
Code:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">
jdbc:mysql://localhost:3307/mql</property>
<property name="connection.username">root</property>
<property name="connection.password">arhid28</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="com/mql/vo/User.hbm.xml"/>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
et finalement ma classe de test:
and finally my test class:
Code:
package com.mql.test;
import org.hibernate.Session;
import com.mql.util.HibernateUtil;
import com.mql.vo.User;
public class TesTHibernate {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
User user = new User();
user.setIdUser(1);
user.setLogin("khadija");
user.setPass("khadija");
session.save(user);
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
}
}
Merci d'avance pour votre aide :)
Thank you in advance for your help :)