gpt4 book ai didi

在 Java Swing 应用程序中关闭 Hibernate Session 时出现 java.lang.NoSuchMethodError

转载 作者:行者123 更新时间:2023-11-29 05:16:00 25 4
gpt4 key购买 nike

我收到这个错误:

Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: org.hibernate.Session.close()Ljava/sql/Connection;

调用此函数时:

private static SessionFactory factory;

public static void insert(Object model) {

factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction();

try {

session.save(model);
tx.commit();

} catch (HibernateException exc) {
if (tx != null)
tx.rollback();
exc.printStackTrace();
} finally {
session.close(); // here is when the error occurs
}
}

我相信我已经从最新的 Hibernate 5.0.1 中的 /lib/required/ 目录导入了所有必需的 jar。

hibernate.cfx.xml 文件也已正确配置:

    <?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="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3307/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property >

<mapping resource="customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>

customer.hbm.xml映射文件:

<class name="com.test.app.model.Customer" table="customer">

<meta attribute="class-description">
Test.
</meta>

<id column="id" name="id" type="int">
<generator class="native"/>
</id>

<property name="name" column="name" type="string"/>
<property name="phoneNumbers" column="phone_numbers" type="string"/>
<property name="emailAddresses" column="email_addresses" type="string"/>
<property name="address" column="address" type="string"/>
<property name="note" column="note" type="string"/>
</class>

可能是什么问题?

最佳答案

看起来您的类路径中有两个 hibernate-core-5.0.1.Final.jar。例如,如果您在 IDE 中运行一个项目并将带有 hibernate-core-5.0.1.Final.jar 的其他项目添加到您的类路径。

附言请不要在每次插入时配置 session 工厂,并在任何异常时回滚,而不仅仅是在 HibernateException 上。您可以看到如何正确处理事务 here ,您可以找到一个使用 Hibernate 5 和 MySQL 的简单示例控制台项目 here .

关于在 Java Swing 应用程序中关闭 Hibernate Session 时出现 java.lang.NoSuchMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32811137/

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