I was trying to write my first Hibernate project. But I got an exception.
java.lang.ClassCastException: org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction cannot be cast to hibernate.HibernateSession
This is how I code session methods.
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class HibernateSession {
static Connection.NewHibernateUtil hibernateutil;
static SessionFactory sessionfactory;
static Session session;
static Transaction transaction;
private HibernateSession() {
}
public static HibernateSession getHSConnection() {
if (transaction == null) {
session = Connection.NewHibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
}
return (HibernateSession) transaction;
}
public static void closeHSConnection() {
if (transaction != null) {
session.flush();
transaction.commit();
}
session.close();
}
}
Are there any errors in this code?