首页 文章

运行程序后,文件数据库中没有模式通过hibernate持久保存到hsqldb:file

提问于
浏览
0

这是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.url">jdbc:hsqldb:file:test_db_file</property>
    <property name="hsqldb.write_delay">false</property>
    <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
    <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
    <property name="hibernate.connection.username">combine1</property>
    <property name="hibernate.connection.password"></property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>
    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>
    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>
    <!--
         few resources
    -->

  </session-factory>
</hibernate-configuration>

当我将hsqldb更改为mysql db时,它工作正常 . 但是当我这样离开它时,我将hsqldb:file作为数据库连接到NetBeans中,在公共模式中没有任何内容,我想在运行程序后找到创建的表 .

在运行时,类是持久的,我可以从会话等加载它们,但是在程序结束后,什么都没有 . 除了test_db_file.script之外,还有用于创建所需模式的查询,但不是我保存到db中的行 .

这是我的简单主要内容,正如我所写,当我使用一些mysql数据库时,它的工作原理如下:

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import resources.HibernateUtil;
import website.Website;

public class MrhAdministration {

  public static void main(String[] args) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();

    WebsiteCreator.create(session);

    Criteria criterial = session.createCriteria(Website.class);
    for (Website w : (List<Website>) criterial.list()) {
      System.out.println("Sites: " + w.getSiteName() + " " + w.getSiteUrl());
    }
    session.getTransaction().commit();
  }
}

谢谢你的回答!

1 回答

  • 0

    最可能的问题是数据库文件的位置 . 您正在使用相对路径 jdbc:hsqldb:file:test_db_file ,当您使用不同的程序访问数据库时,它会解析到不同的位置 . 尝试绝对路径,例如 jdbc:hsqldb:file:/db/test_db_file

    请注意,您对username和write_delay的其他设置对于最新版本2.2.x及更高版本有效 . 它们不适用于1.8.x版 .

相关问题