在Java Swing应用程序中关闭Hibernate会话时出现java.lang.NoSuchMethodError [英] java.lang.NoSuchMethodError when closing Hibernate Session in Java Swing application

查看:116
本文介绍了在Java Swing应用程序中关闭Hibernate会话时出现java.lang.NoSuchMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

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.

I believe I have imported all the necessary jars from /lib/required/ directory in the latest Hibernate 5.0.1.

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将其他项目添加到类路径中.

Looks like you have two hibernate-core-5.0.1.Final.jar in your classpath. For example, if you run a project in the IDE and add other project with hibernate-core-5.0.1.Final.jar to your classpath.

P.S.请不要在每个插入中都配置会话工厂,并回滚任何异常,而不仅限于HibernateException.您可以看到如何正确处理事务

P.S. Please don't configure the session factory in every insert, and rollback on any exception, not only on HibernateException. You can see how properly working with transactions here, and you can find a simply example console project using Hibernate 5 and MySQL here.

这篇关于在Java Swing应用程序中关闭Hibernate会话时出现java.lang.NoSuchMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆