使用Sqlite的Hibernate 5.0.2 - 由于错误而无法使其正常工作

时间:2016-11-21 21:59:57

标签: java hibernate sqlite jdbc

在研究Hibernate / JPA / ORM时,我从网上找到了一个HibernateHelloWorld Java应用程序。

通过Maven我使用这些库:

   <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.0.2.Final</version>
    </dependency>
    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.15.0</version>
    </dependency>

[1]运行应用程序时,我的第一个问题是无法创建任何表。好的,我创建了表格。

[2]第二个问题是提交无法完成。

[3]第三个问题是数据库一直处于锁定状态。

hibernate配置文件是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
"http://www.hibernate.org/dtd/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="nl.deholtmans.HibernateHelloWorld.Contact"/>
    </session-factory>
</hibernate-configuration>

带注释的POJO是:

@Entity
@Table(name = "contact")
public class Contact {
    private Integer id;
    private String name;
    private String email;

    public Contact() {

    }

    public Contact(Integer id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
    }

    @Id
    public Integer getId() {
        return this.id;
    }
    // etc. 

简单的HibernateHelloWorld应用是这样的:

public class App {
    private static SessionFactory sessionFactory = null;  
    private static SessionFactory configureSessionFactory() throws HibernateException {  
        sessionFactory = new Configuration()
            .configure() 
            .buildSessionFactory();
        return sessionFactory;
    }

    public static void main(String[] args) {
        configureSessionFactory();
        Session session = null;
        Transaction tx=null;
        try {
            session = sessionFactory.openSession();
            tx = session.beginTransaction();

            Contact myContact = new Contact(202, "My Name", "my_email@email.com");
            Contact yourContact = new Contact(203, "Your Name", "your_email@email.com");

            session.save(myContact);
            session.save(yourContact);
            session.flush();
            tx.commit();

            List<Contact> contactList = session.createQuery("from Contact").list();
            for (Contact contact : contactList) {
                System.out.println("Id: " + contact.getId() + " | Name:"  + contact.getName() + " | Email:" + contact.getEmail());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tx.rollback();
        } finally{
            if(session != null) {
                session.close();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

为了让Hibernate与Sqlite结合使用,我终于找到了:

[1]我使用了更新版本的hibernate-core:5.1.2。

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.1.2.Final</version>
    </dependency>

[2]我使用了以下方言和身份文件:

https://github.com/gwenn/sqlite-dialect/tree/master/src/main/java/org/hibernate/dialect

[3]我在POJO中添加了列名。我想这不是主要的突破。所以,例如:

@Entity
@Table(name = "contact")
public class Contact {

    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;

    @Column(name = "email")
    private String email;

    public Contact() {
    }
    // etc. 

==&GT;我希望Hibernate的创建者可以添加一个关于将Hibernate与Sqlite集成的简单而好的教程。

答案 1 :(得分:0)

您的数据库似乎已被锁定。我找到了这个话题:

Getting [SQLITE_BUSY] database file is locked with select statements

希望这有帮助。