![](/img/trans.png)
[英]HSQLDB & DBUnit, Cannot find column from ResultSetMetaData when using allias on SQL query
[英]How to exclude a column during INSERT with dbunit to HSQLDB
我將數據從MS SQLServer導出到xml文件,然后在需要數據庫的運行單元測試中使用該數據集。 我為此使用dbunit maven插件。
對我來說不幸的是,並非某些表中的所有列都映射到我的Entity類中。
舉例來說,我們有一個名為“ member”的表。 成員表具有三列:memberid,membername,memberrank。 導出時,將導出所有三列。 但是在我的MemberEntity類中,我僅映射memberid和membername,因為在我的應用程序中不需要memberrank。 所以我將讓MemberEntity看起來像這樣:
@Entity
@Table(name = "member")
public class MemberEntity {
@Id
@GeneratedValue()
@Column(name = "memberid", nullable = false)
private Integer memberid;
@Column(name = "membername", nullable = false)
private String membername;
...
}
然后,我嘗試在測試用例之前將數據集插入HSQLDB:
IDatabaseConnection conn = new DatabaseConnection(((SessionImpl) (entityManager.getDelegate())).connection());
IDataSet dataset = new XmlDataSet(
resourceLoader.getResource("classpath:dataset.xml").getInputStream());
conn.getConfig().setProperty("http://www.dbunit.org/properties/datatypeFactory", new MsSqlDataTypeFactory());
DatabaseOperation.CLEAN_INSERT.execute(conn, dataset);
在這一點上,我得到一個例外,說該列MemberRank不存在。 它說如下:
org.dbunit.dataset.NoSuchColumnException: MEMBER.MEMBERRANK - (Non-uppercase input column: memberrank) in ColumnNameToIndexes cache map. Note that the map's column names are NOT case sensitive.
當我從數據集中刪除該列時,一切都很好。 如果我將memberRank映射添加到我的Entity類,則一切順利。 但是我無法將列映射添加到我的Entity類中。 是否有一種簡便的方法(除了手動從導出的數據集中刪除列和關聯的數據之外),當我執行INSERT時排除(嘗試)添加該列?
在休眠狀態下,實體的每個非靜態非瞬時屬性(取決於訪問類型的字段或方法)都被視為持久性的,除非將其注釋為@Transient。
例如,
@Transient
public int counter; //transient property
private String firstname; //persistent property
注釋為@Transient的方法和字段將被實體管理器忽略。有關更多信息,請參見此處 。
也許這個答案來得有點晚,但是我遇到了一個類似的問題,並編寫了以下方法來解決它(我使用的是dbUnit 2.5.0)。 希望它能幫助到別人。
/**
* Generates a new data set with the columns declared in the
* "excludedColumns" map removed.
*
* @param src
* Source data set.
* @param excludedColumns
* Map of table names and column names. Columns in this map are
* removed in the resulting data set.
* @return Data set with the columns declared in the "excludedColumns" map
* removed. Tables that are not specified in the "excludedColumns"
* map are left untouched.
* @throws DataSetException
*/
public static IDataSet filterDataSet(IDataSet src,
Map<String, Set<String>> excludedColumns) throws DataSetException {
if (excludedColumns == null) {
return src;
}
ArrayList<ITable> tables = new ArrayList<ITable>(
src.getTableNames().length);
for (String tableName : src.getTableNames()) {
if (excludedColumns.containsKey(tableName)) {
ITable filteredTable = DefaultColumnFilter
.excludedColumnsTable(
src.getTable(tableName),
excludedColumns.get(tableName).toArray(
new String[0]));
tables.add(filteredTable);
} else {
tables.add(src.getTable(tableName));
}
}
return new DefaultDataSet(tables.toArray(new ITable[0]),
src.isCaseSensitiveTableNames());
}
該方法的核心是DefaultColumnFilter
。 我在這里使用商品靜態方法,但是DefaultColumnFilter
的實例提供了很大的靈活性。
我想知道是否有更直接的方法來做到這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.