java - Hibernate + SQLite 不创建表

标签 java hibernate sqlite netbeans

我的项目中需要SQLite数据库,但是SQL不如HQL方便。所以我们有 Hibernate 和 SQLite。 项目是在 NetBeans 8.2 中制作的,我的操作系统是 Ubuntu 17.10。

例如,我们有一个用于测试的小型项目

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.sqlite.JDBC</property>
    <property name="hibernate.connection.url">jdbc:sqlite:stuff.db</property>
    <property name="hibernate.dialect">com.enigmabridge.hibernate.dialect.SQLiteDialect</property>
    <property name="hibernate.hbm2ddl.auto">create</property>
    <property name="hibernate.show_sql">true</property>
    <mapping resource="stuff/Stuff.hbm.xml"/>

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

我的类(class)名为“Stuff”:

package stuff;

public class Stuff {
    private Integer id;
    private String stuff;

    public Stuff() {
    }

    public Stuff(String stuff) {
        this.stuff = stuff;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getStuff() {
        return stuff;
    }

    public void setStuff(String stuff) {
        this.stuff = stuff;
    }

    @Override
    public String toString() {
        return "Stuff{" + "id=" + id + ", stuff=" + stuff + '}';
    }

}

映射文件

<hibernate-mapping>
  <class name="stuff.Stuff" table="stuff">
      <id name="id" type="integer" column="id">
          <generator class="increment"/>
      </id>
      <property name="stuff" type="string" column="stuff"/>

  </class>
</hibernate-mapping>

以及主要代码

package stuff;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

public class Main {

    public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(new Stuff("blah blah"));

        //Query q = s.createQuery("select from Stuff");
        //List<Stuff> l = q.list();
        //for (Stuff s:l)System.out.println(s);
        session.getTransaction().commit();
        session.close();
        System.exit(0);
    }

}

数据库是通过 NetBeans 中的“服务”选项卡创建的。它的名字是stuff.db。

我使用 Hibernate 4.3 (JPA 2.1)

Hibernate 4 SQLite 方言取自此处:link

session 打开和关闭都很好,数据库文件会在我的主文件夹中自动创建。但由于某种原因,表没有创建,我真的不知道为什么。

最佳答案

pom:SQLite JDBC 库

<dependency>
    <groupId>org.xerial</groupId>
    <artifactId>sqlite-jdbc</artifactId>
    <version>3.7.2</version>
</dependency>

在 hibernate 配置中:

<?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="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="dialect">org.hibernate.dialect.SQLiteDialect</property>
        <property name="connection.driver_class">org.sqlite.JDBC</property>
        <property name="connection.url">jdbc:sqlite:mydb.db</property>
        <property name="connection.username"></property>
        <property name="connection.password"></property>

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

        <mapping class="your mapping class"/>
    </session-factory>
</hibernate-configuration>

关于java - Hibernate + SQLite 不创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48414625/

相关文章:

java - Netbeans 8.2 : How to stop embedded Tomcat in Spring. 在 osx 上引导项目?

java - 从列表中删除条目?

java - 将对象从 JSP 传递到 Spring Controller

c# - 如果不存在,EF7 sqlite 创建表

java - 使用Java按顺序运行命令行命令

java - 每月 hibernate 表

java - Hibernate没有注释实体

java - 无法创建未找到的唯一键约束

android - 关于 Android SQLite 中的 "_id"字段

sql - 为什么SQLite索引不能加快查询速度?