【问题标题】:Hibernate 5.0.2 with Sqlite -- cannot get it working due to errors带有 Sqlite 的 Hibernate 5.0.2 - 由于错误而无法正常工作
【发布时间】:2016-11-21 21:59:56
【问题描述】:

在研究 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] 第三个问题是数据库一直被锁定。

休眠配置文件为:

<?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();
            }
        }
    }
}

【问题讨论】:

  • 我注意到您的代码的第一件事是没有映射文件。通常,休眠应用程序将具有 hibernate.hbm.xml 文件。此文件被传递到configure() methods of the Configuration class 之一。
  • Consider looking at this post 开始了解 Hibernate 如何为您生成数据库模式。它不会自动执行。
  • 为简洁起见,我跳过了 Contact.java (POJO) 和休眠配置文件。现在已添加它们。

标签: java hibernate sqlite jdbc


【解决方案1】:

为了让 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. 

==> 希望Hibernate的创造者可以添加一个简单而好的集成Hibernate和Sqlite的教程。

【讨论】:

    【解决方案2】:

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

    Getting [SQLITE_BUSY] database file is locked with select statements

    希望这会有所帮助。

    【讨论】:

    • 这是运行这个Hello World程序的结果。我什么都不做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 2020-03-03
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多