Hibernate + SQLite不会创建表

时间:2018-01-24 04:02:27

标签: java hibernate sqlite netbeans

我的项目中需要SQLite数据库,但SQL并不像HQL那样方便。所以我们在SQLite中使用了Hibernate。 项目正在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>

我的课叫做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

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

1 个答案:

答案 0 :(得分:0)

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>