gpt4 book ai didi

java - 运行 jsp 时无法转换为 hibernate.Hibernate Session

转载 作者:行者123 更新时间:2023-11-30 06:44:30 25 4
gpt4 key购买 nike

<分区>

我正在尝试编写我的第一个 Hibernate 项目。但我有一个异常(exception)。

java.lang.ClassCastException: org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction cannot be cast to hibernate.HibernateSession

这就是我编写 session 方法的方式。

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();
}
}

这段代码有没有错误?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com