2012-01-10 49 views
0

我将数据从MS SQLServer导出到xml文件,然后在运行需要数据库的单元测试中使用该数据集。我使用dbunit maven插件。如何在使用dbunit插入期间将列排除到HSQLDB

对我来说不幸的是,并不是所有表中的所有列都映射到我的实体类中。例如,我们有一个名为'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映射添加到我的实体类中,则一切顺利。 但我无法将列映射添加到我的实体类中。是否有一种简单的方法(除了手动从导出的数据集中删除列和关联的数据)排除该列时(我试图添加)在执行INSERT时?

+0

是您的问题解决了吗?我看不到任何评论或接受的答案。 – ManuPK 2012-01-18 15:02:01

回答

1

在休眠实体的每个非静态非瞬态属性(取决于访问类型的字段或方法)被视为持久化,除非您将其注释为@Transient。

例如,

@Transient 
public int counter; //transient property 

private String firstname; //persistent property 

的方法和注释为@Transient字段将由实体manager.See here以获取更多信息被忽略。

+1

这不是我正在寻找的东西。我曾经想过让DBUnit忽略某些列,而不修改我的实体类。尽管如此,我最终还是在实体类中添加了这些列。 – 2012-01-18 22:28:30

0

也许这个答案有点晚,但我刚刚遇到类似的问题,并写了下面的方法来解决它(我使用的是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的实例给出了很大的灵活性。

我不知道是否有更直接的方式来做到这一点。

相关问题