0

参考这些教程: 教程 1

教程 2

这些都不是最新的。因此,如果我修复了一个错误,我就会得到下一个错误。也许有人可以帮忙。

pom.xml,我看了好几遍,但也许有一个库仍然不是最新的或被遗忘了。

 <project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.hibernate.tutorials</groupId>
<artifactId>hibernate-tutorial</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>First Hibernate Tutorial</name>

<build>
     <!-- we dont want the version to be part of the generated war file name -->
     <finalName>${project.artifactId}</finalName>
</build>

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.1.3.Final</version>
    </dependency>

    <!-- Because this is a web app, we also have a dependency on the servlet api. -->
   <dependency>
        <groupId>org.apache.tomcat</groupId>
<artifactId>servlet-api</artifactId>
<version>6.0.35</version>
  </dependency>

    <!-- Hibernate uses slf4j for logging, for our purposes here use the simple backend -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.6.4</version>
    </dependency>

    <!-- Hibernate gives you a choice of bytecode providers between cglib and javassist -->
    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.12.1.GA</version>
    </dependency>

    <!-- HSQLDB -->
      <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>2.0.0</version>
    </dependency>

</dependencies>

休眠.cfg.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.connection.driver_class">org.hsqldb.jdbcDriver</property>
    <property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost</property>
    <property name="hibernate.connection.username">sa</property>

    <property name="connection.password"></property>

 <property name="connection.pool_size">1</property>

 <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

 <property name="show_sql">true</property>

 <property name="hbm2ddl.auto">update</property>



 <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>
</session-factory>
 </hibernate-configuration>

主要的

 package org.hibernate.tutorial.domain;

 import java.util.Date;

 import org.hibernate.Session;
 import org.hibernate.tutorial.util.HibernateUtil;

 public class EventManager {

public static void main(String[] args) {
    EventManager mgr = new EventManager();


    Session session = HibernateUtil.buildSessionFactory().getCurrentSession();
    session.beginTransaction();

    Event theEvent = new Event();
    theEvent.setTitle("My Event");
    theEvent.setDate(new Date());
    session.save(theEvent);

    session.getTransaction().commit();


    session.close();
  }
 }

Util,我做了一些改变

 import org.hibernate.SessionFactory;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.service.ServiceRegistry;
 import org.hibernate.service.ServiceRegistryBuilder;


 public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

public static SessionFactory buildSessionFactory() {
    try {
        // Create the SessionFactory from hibernate.cfg.xml
        Configuration configuration = new Configuration();
        configuration.configure();
        serviceRegistry = new      ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();     
        return new Configuration().configure().buildSessionFactory(serviceRegistry);
    } catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}
 }

我希望这是正确的方法:在我开始我的主要工作之前,我通过以下方式启动 hsqldb 服务器

 mvn exec:java -Dexec.mainClass="org.hsqldb.Server" -Dexec.args="file:target/data/tutorial"

然后我开始我的主要方法:

 mvn exec:java -Dexec.mainClass="org.hibernate.tutorial.EventManager"

我的最后一个错误是:

org.hibernate.SessionException:会话已经关闭

4

1 回答 1

0

由于没有将会话绑定到会话上下文的机制,您应该只使用 openSession() 来获取新会话。

如果您开始以更严肃的方式使用 Hibernate,您可以指定会话上下文以确保可以从 getCurrentSession() 获取会话

于 2012-06-16T16:58:42.443 回答