2 pièce(s) jointe(s)
Erreur lors de l'exécution d'Hibernate
Bonjour,
Je suis entrain d'écrire une petite application utilisant hibernate. Je fais la configuration mais lors de l'exécution le fichier est introuvable.
Code:
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
|
oct. 12, 2018 11:13:02 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.3.6.Final}
oct. 12, 2018 11:13:02 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
oct. 12, 2018 11:13:02 AM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
Hibernate Configuration loaded
Hibernate serviceRegistry created
oct. 12, 2018 11:13:03 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
oct. 12, 2018 11:13:03 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
oct. 12, 2018 11:13:03 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/univreport]
oct. 12, 2018 11:13:03 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
oct. 12, 2018 11:13:03 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
oct. 12, 2018 11:13:03 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
oct. 12, 2018 11:13:03 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
oct. 12, 2018 11:13:03 AM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
oct. 12, 2018 11:13:03 AM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@9573584'
Exception in thread "main" java.lang.IllegalStateException: Transaction not successfully started
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:92)
at dao.CoursImp.addCours(CoursImp.java:29)
at dao.Test.main(Test.java:15) |
La structure de mon projet est la suivante:
Pièce jointe 418986
la configuration est la suivante:
Code:
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
|
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost/univreport</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hbm2ddl.auto">create</property>
<property name="current_session_context_class">thread</property>
<!-- this will show us all sql statements -->
<property name="show_sql">true</property>
<!-- mapping files -->
<mapping class="Entity.Cours" />
<mapping class="Entity.Etudiant" />
</session-factory>
</hibernate-configuration> |
HibernateUtil:
Code:
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 57 58 59 60 61 62 63 64 65 66
|
package util;
import java.util.Properties;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
//XML based configuration
private static SessionFactory sessionFactory=buildSessionFactory();
private static ServiceRegistry serviceRegistry;
private static Session session=null;
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure("config/hibernate.cfg.xml");
System.out.println("Hibernate Configuration loaded");
serviceRegistry = new StandardServiceRegistryBuilder().
applySettings(configuration.getProperties()).build();
System.out.println("Hibernate serviceRegistry created");
return configuration.buildSessionFactory(serviceRegistry);
}
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;
}
public static Session openSession() {
return sessionFactory.openSession();
}
public static Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
public static void close() {
if(sessionFactory!=null) {
sessionFactory.close();
}
sessionFactory=null;
}
} |
Entity Cours:
Code:
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 57 58 59 60 61 62 63 64 65 66 67 68 69
| package Entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="Cours")
public class Cours implements Serializable {
@Id
private String codecours;
private String designation;
private int ponderation;
private String nombreHeure;
public Cours() {
super();
// TODO Auto-generated constructor stub
}
public Cours(String codecours, String designation, int ponderation, String nombreHeure) {
super();
this.codecours = codecours;
this.designation = designation;
this.ponderation = ponderation;
this.nombreHeure = nombreHeure;
}
public Cours(String codecours) {
super();
this.codecours = codecours;
}
public String getCodecours() {
return codecours;
}
public void setCodecours(String codecours) {
this.codecours = codecours;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public int getPonderation() {
return ponderation;
}
public void setPonderation(int ponderation) {
this.ponderation = ponderation;
}
public String getNombreHeure() {
return nombreHeure;
}
public void setNombreHeure(String nombreHeure) {
this.nombreHeure = nombreHeure;
}
} |
CoursImp:
Code:
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| package dao;
import java.util.List;
import org.hibernate.Session;
import Entity.Cours;
import util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class CoursImp implements ICours{
//création objet session Hibernate
static Session session=HibernateUtil.openSession();
@Override
public void addCours(Cours co) {
//debut d'une transaction
session.beginTransaction();
try {
//enregistrement
session.save(co);
} catch (Exception e) {
session.getTransaction().rollback();
}
//valide la transaction
session.getTransaction().commit();
}
@Override
public List<Cours> listCours() {
return session.createQuery("select c from Cours c").list();
}
@Override
public Cours getCours(String codecours) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Cours> listCoursParMC(String mc) {
// TODO Auto-generated method stub
return null;
}
@Override
public void deleteCours(String codecours) {
//debut d'une transaction
session.beginTransaction();
try {
//suppression
Cours co=findById(codecours);
session.delete(co);
} catch (Exception e) {
session.getTransaction().rollback();
}
//valide la transaction
session.getTransaction().commit();
}
@Override
public void updateCours(Cours co) {
//debut d'une transaction
session.beginTransaction();
try {
//modification
session.merge(co);
} catch (Exception e) {
session.getTransaction().rollback();
}
//valide la transaction
session.getTransaction().commit();
}
@Override
public Cours findById(String mat) {
return (Cours)session.get(Cours.class,mat);
}
} |
Classe Test:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package dao;
import org.hibernate.Session;
import Entity.Cours;
import util.HibernateUtil;
public class Test {
static Session session=HibernateUtil.openSession();
public static void main(String[] args) {
CoursImp metier=new CoursImp();
metier.addCours(new Cours("J1","JAVA 1",4,"60"));
session.createQuery("select c from Cours c").list();
}
} |
Comment résoudre le problème?