使用DBUnit在内存数据库中创建表时出错

时间:2013-06-14 01:50:23

标签: java xml hibernate mapping dbunit

我正在尝试将数据和表从XML文件导入到内存数据库,这是我使用DBUnit自动创建的XML文件。这是我映射的两个类: WorkbookMapping:

@Entity(name="Workbook")
public class WorkbookMapping implements Serializable {
@Id
@GeneratedValue
private int id;

@OneToMany(cascade = CascadeType.ALL )
List<SpreadSheetMapping> list = new ArrayList<SpreadSheetMapping>();

public WorkbookMapping() {
}

public WorkbookMapping(Workbook workbook) {
    for (int i = 0; i < workbook.getSpreadsheetCount(); i++) {
        list.add(new SpreadSheetMapping(workbook.getSpreadsheet(i)));
    }

}
}

SpreadSheetMapping:

@Entity(name="Spreadsheet")
public class SpreadSheetMapping implements Serializable {
@Id
@GeneratedValue
private long id;

@OneToMany(cascade = CascadeType.ALL)
List<CellMapping> list;

public SpreadSheetMapping() {
}

public SpreadSheetMapping(Spreadsheet sp) {
    list = new ArrayList<CellMapping>();
    for (int r = 0; r < sp.getRowCount(); r++) {
        for (int i = 0; i < sp.getColumnCount(); i++) {
             list.add(new CellMapping(sp.getCell(i, r)));
        }
    }
}
}

这是我的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="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:mem:DBMemoria</property>
<property name="connection.username"/>
<property name="connection.password"/>
<property name="connection.pool_size">1</property>
<mapping class="csheets.io.WorkbookMapping"/>
<mapping class="csheets.io.SpreadSheetMapping"/>
<mapping class="csheets.io.CellMapping"/>
</session-factory>
</hibernate-configuration>

这是我用来将所有数据导入数据库的代码:

Connection jdbcConnection;
jdbcConnection = DriverManager.getConnection("jdbc:hsqldb:mem:DBMemoria", "", "");
IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
IDataSet dataSet = new XmlDataSet(stream);
DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);

我收到了这个错误:

org.dbunit.dataset.NoSuchTableException: WORKBOOK_SPREADSHEET   
at org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:288)
at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:109)

任何帮助?

2 个答案:

答案 0 :(得分:0)

您的实体不适用于您在导入文件中定义的表。你应该添加

@Entity
@Table(name = "WORKBOOK_SPREADSHEET")

适当的实体。

答案 1 :(得分:0)

@Adam,我认为我们需要查看XML DataSet。正如@Aleksandra提到的那样,你没有创建一个WORKBOOK_SPREADSHEET表。必须将实体映射到XmlDataSet中的表。