【问题标题】:java.lang.NoSuchMethodError when closing Hibernate Session in Java Swing application在 Java Swing 应用程序中关闭 Hibernate 会话时出现 java.lang.NoSuchMethodError
【发布时间】:2015-12-24 23:53:27
【问题描述】:

我收到此错误:

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>

可能是什么问题?

【问题讨论】:

  • 您的项目可能还有哪些其他库?
  • 好吧,我也有 Absolute Layout 和 MySQL JDBC Driver。

标签: java mysql swing hibernate orm


【解决方案1】:

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

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

【讨论】:

  • @user2200690 谢谢。固定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-20
相关资源
最近更新 更多