gpt4 book ai didi

java - Hibernate + SQLite 不创建表

转载 作者:太空宇宙 更新时间:2023-11-04 10:45:58 24 4
gpt4 key购买 nike

我的项目中需要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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com