存储和对象在内存数据库中

时间:2013-06-08 18:41:15

标签: java database hibernate mapping

我正在尝试将一个对象Ficheiro存储在我的内存数据库中。该对象包含:

private int version;  
private Workbook content;

这是我用来存储在数据库中的代码:

    HSQLServerUtil.getInstance().start("DBMemoria");

    Ficheiro fich = new Ficheiro();
    fich.setVersion(1);
    fich.setContent(workbook);
    byte[] by = serialize(fich);

    try {
        Blob blob = new SerialBlob(by);
        Session ss = HibernateUtils.newSessionFactory("csheets\\ext\\Hibernate\\hibernate.cfg.xml").openSession();
        ss.beginTransaction();
        ss.getTransaction();
        ss.save(workbook);
        ss.getTransaction().commit();
        ss.close();
    } catch (SQLException ex) {
        System.out.println("ERROR");
    }

这是我的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.hsqldb.jdbcDriver</property>
    <property name="hibernate.hbm2ddl.auto">create</property>
    <property name="hibernate.connection.url">jdbc:hsqldb:file:C:\Users\Adam\Documents\NetBeansProjects\lapr4_2dd_g1_s2\lib\Version</property>
    <property name="hibernate.connection.username"/>
    <property name="hibernate.connection.password"/>
    <mapping resource="csheets/ext/Hibernate/File.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

最后这是我的File.hbm.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class dynamic-insert="false" dynamic-update="false" mutable="true" name="csheets.ext.Hibernate.Ficheiro" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="File">
    <id column="version" name="version" type="int" unsaved-value="0">
      <generator class="identity">  
        </generator>
    </id>
    <property column="content" name="content" type="blob"/>
    <!--<property column="name" name="name" type="string"/>-->
  </class>
</hibernate-mapping>

我收到以下错误:

Exception in thread "AWT-EventQueue-0" org.hibernate.MappingException: Unknown entity: csheets.core.Workbook

我已经在Workbook类中添加了@Entity。谁知道什么是错的? 如果可能的话,你能告诉我我是否这样做了吗?

由于

1 个答案:

答案 0 :(得分:0)

将hibernate配置更新为以下内容以使其正常工作(对于开发机器):

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