Erreur lors de l'exécution d'Hibernate
Bonsoir,
Je suis encore un débutant dans hibernate,j'ai crée mon premier projet pour le teste voici donc mes fichiers:
HibernateSessionFactory.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package Modele.dao;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static final SessionFactory sessionFactory;
static
{
try
{
// Crée lobjet SessionFactory à partir de hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex)
{
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
} |
EtudiantDAO.java
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
| package Modele.dao;
import java.util.*;
import org.hibernate.Session;
import org.hibernate.Transaction;
import Modele.Etudiant;
import Modele.dao.HibernateSessionFactory;
public class EtudiantDAO {
public static void main(String [] args){
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();
//On insère 2 etudiant dans la BDD
Etudiant eleve = new Etudiant();
eleve.setNom("Durand");
eleve.setPrenom("Jacques");
eleve.setAge(new Integer(20));
session.save(eleve);
eleve = new Etudiant();
eleve.setNom("Petit");
eleve.setPrenom("Lucie");
eleve.setAge(new Integer(19));
session.save(eleve);
tx.commit();
List list = session.createQuery("from Etudiant").list();
Iterator it = list.iterator();
while(it.hasNext())
{
Etudiant e = (Etudiant)it.next();
System.out.println(e.getPrenom()+" "+ e.getNom()+": "+ e.getAge()+" ans");
}
HibernateSessionFactory.closeSession();
}
} |
et voici les erreurs que j'ai trouvé lors de l'exécution:
Citation:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method currentSession() is undefined for the type HibernateSessionFactory
The method closeSession() is undefined for the type HibernateSessionFactory
at Modele.dao.EtudiantDAO.main(EtudiantDAO.java:9)
Donc comment je dois résoudre ces erreurs?
Et merci d'avance pour vos réponses.