Hola,
Ya hice el ejemplo pero me marca error en la clase (Ejemplo1) las parte subrayada.
Me presenta este error "The method getCurrentSession() is undefined for the type"
Esperando me puedas ayudar.
Muchas gracias y saludos.
private Long createAndStoreEvent(String title, Date theDate) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
session.getTransaction().commit(); log.info("Insertado: "+theEvent);
return theEvent.getId();
}
private List<Event> listEvents() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<Event> result = (List<Event>)session.createQuery("from Event").list();
session.getTransaction().commit();
for (Event evento : result) {
log.info("Leido: "+evento);
}
return result;
}
**********
package com.chuidiang.ejemplos.hibernate.ejemplo1;
import java.io.*;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure(new File("hibernate1.cfg.xml")).buildSessionFactory();
} 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;
}
}
Ya hice el ejemplo pero me marca error en la clase (Ejemplo1) las parte subrayada.
Me presenta este error "The method getCurrentSession() is undefined for the type"
Esperando me puedas ayudar.
Muchas gracias y saludos.
private Long createAndStoreEvent(String title, Date theDate) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
session.getTransaction().commit(); log.info("Insertado: "+theEvent);
return theEvent.getId();
}
private List<Event> listEvents() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<Event> result = (List<Event>)session.createQuery("from Event").list();
session.getTransaction().commit();
for (Event evento : result) {
log.info("Leido: "+evento);
}
return result;
}
**********
package com.chuidiang.ejemplos.hibernate.ejemplo1;
import java.io.*;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure(new File("hibernate1.cfg.xml")).buildSessionFactory();
} 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;
}
}