如何使用SQLite和Hibernate插入

时间:2015-02-24 11:42:46

标签: java hibernate sqlite

我试图用SQLite和Hibernate创建一个应用程序。

现在我可以从我的SQLite DB中获取一个列表。

但是我得到一个错误,例如"错误:HHH000299:无法完成架构更新"当我的申请被执行时。

插入数据时我无法提交。

Pleanse给我一些建议。

我创建的源代码如下。

的hibernate.cfg.xml          

<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:db/ProductInfo.db</property>
    <property name="connection.username"></property>
    <property name="connection.password"></property>

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

    <!--<mapping class="jp.sigre.WantAll.ProductInfoBean"/> -->
    <!-- Mapping files -->
    <mapping resource="ProductInfo.hbm.xml" />
</session-factory>

ProductInfo.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <class name="jp.sigre.WantAll.ProductInfoBean" table="ProductInfo">
    <id name="id" column="Id" type="int" unsaved-value="null">
        <generator class="identity" />
    </id>
    <property name="title"  column="Title"  type="string" unique="true" not-null="true"/>
    <property name="author" column="Author" type="string" />
    <property name="url"    column="URL"    type="string"/>
    <property name="releaseDate"    column="ReleaseDate"    type="int"/>
    <property name="flag"   column="Flg"    type="int" not-null="true"/>
    </class>
</hibernate-mapping>

ProductInfoBean.java as Entity

/**
 *
 */
package jp.sigre.WantAll;

@Entity
@Table(name = "ProductInfo")
public class ProductInfoBean {

private int    id = -1;
private String title;
private String author;
private String url;
private int    releaseDate = 18000101;
private int    flag; //表示要否etc

public ProductInfoBean(int id, String title, String author, String url, int releaseDate, int flag) {
    setId(id);
    setTitle(title);
    setAuthor(author);
    setUrl(url);
    setReleaseDate(releaseDate);
    setFlag(flag);
}

public ProductInfoBean() {
}

@Id
public int getId() {
    return id;
}

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


public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public int getReleaseDate() {
    return releaseDate;
}

public void setReleaseDate(int releaseDate) {
    this.releaseDate = releaseDate;
}

@Column(name = "flg")
public int getFlag() {
    return flag;
}

public void setFlag(int flag) {
    this.flag = flag;
}

@Transient
public String[] getInfoAsAry() {
    String id     = String.valueOf(getId() );
    String title  = (getTitle()      != null) ? getTitle()  : "";
    String author = (getAuthor()     != null) ? getAuthor() : "";
    String url    = (getUrl()        != null) ? getUrl()    : "";
    String date   = String.valueOf(getReleaseDate());
    String flag   = String.valueOf(getFlag());
    String[] result = {id, title, author, url, date, flag};
    return result;
}

@Override
public String toString() {
    return "ProductInfoBean [id=" + id + ", title=" + title + ", author="
            + author + ", url=" + url + ", releaseDate=" + releaseDate
            + ", flag=" + flag + "]";
}

试图插入的代码。

    private static SessionFactory configureSessionFactory() throws HibernateException {
    Configuration configuration = new Configuration().configure();

    //Properties properties = configuration.getProperties();

    //serviceRegistry = new ServiceRegistryBuilder().applySettings(properties).buildServiceRegistry();
    sessionFactory = configuration.buildSessionFactory();

    return sessionFactory;
}

    public int insertProductInfo(ProductInfoBean info) {
    Transaction trans = null;
    try {
        session = sessionFactory.openSession();

        trans = session.beginTransaction();
        System.out.println(info.toString());
        session.save(info);
        // Committing the change in the database.
        //session.flush();
        trans.commit();
    } catch (Exception e) {
        System.out.println("test");
        trans.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }

    return 1;
}

我使用方言课。 https://github.com/gwenn/sqlite-dialect/blob/master/src/main/java/org/hibernate/dialect/SQLiteDialect.java

表由此SQL创建。

CREATE TABLE `ProductInfo` (
`Id`    INTEGER,
`Title` TEXT NOT NULL UNIQUE,
`Author`    TEXT,
`URL`   TEXT,
`ReleaseDate`   INTEGER DEFAULT 18000101,
`Flg`   INTEGER NOT NULL,
PRIMARY KEY(Id)
);

感谢。

0 个答案:

没有答案