繁体   English   中英

如何在将dbunit插入HSQLDB的过程中排除列

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM