【问题标题】:Hibernate + HSQDL beginnerHibernate + HSQDL 初学者
【发布时间】:2012-06-16 13:39:05
【问题描述】:

参考这些教程: Tutorial 1

Tutorial 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>

hibernate.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;
}
 }

我希望这是正确的方法:在我启动我的 main 之前,我通过

启动 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: 会话已经关闭

【问题讨论】:

  • 尝试使用 sessionFactory.openSession() 代替吗?
  • 使用 HibernateUtil.getSessionFactory().close();现在。但是现在我遇到了另一个错误:无法执行目标 org.codehaus.mojo:exec-maven-plugin:1.2.1:java on project hibernate-tutorial:执行 Java 类时发生异常。空:InvocationTargetException:NullPointerException
  • HibernateUtil.getSessionFactory().close();抛出 NullpointerException
  • 现在使用:Session session = HibernateUtil.buildSessionFactory().openSession(); session.close();工作正常,但我读到使用 currentSession 的 openSession inseatd 不好
  • 我认为您不必担心自己编写的代码。我会发布一个答案,以便您接受。

标签: hibernate hsqldb


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 2022-01-17
    • 2012-12-20
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多