简体   繁体   English

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

[英]How to exclude a column during INSERT with dbunit to HSQLDB

I export data from MS SQLServer to an xml file, then use that dataset in running unit tests that require database. 我将数据从MS SQLServer导出到xml文件,然后在需要数据库的运行单元测试中使用该数据集。 I use dbunit maven plugin for it. 我为此使用dbunit maven插件。

Unfortunately for me, not all columns in some tables are mapped in my Entity classes. 对我来说不幸的是,并非某些表中的所有列都映射到我的Entity类中。

As an example, say, we have a table called 'member'. 举例来说,我们有一个名为“ member”的表。 Member table has three columns: memberid, membername, memberrank. 成员表具有三列:memberid,membername,memberrank。 When I do an export, I get all three columns exported. 导出时,将导出所有三列。 But in my MemberEntity class, I only map memberid and membername, because I do not need memberrank in my application. 但是在我的MemberEntity类中,我仅映射memberid和membername,因为在我的应用程序中不需要memberrank。 So I would have the MemberEntity looking like this: 所以我将让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;
...
}

Then, I try to insert dataset into HSQLDB before a test case: 然后,我尝试在测试用例之前将数据集插入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);

At this point, I get an exception saying the column MemberRank does not exist. 在这一点上,我得到一个例外,说该列MemberRank不存在。 It says something like the following: 它说如下:

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.

When I remove the column from the dataset, all is well. 当我从数据集中删除该列时,一切都很好。 If I add in the memberRank mapping to my Entity class, again, all goes well. 如果我将memberRank映射添加到我的Entity类,则一切顺利。 But I cannot add the column mapping into my Entity class. 但是我无法将列映射添加到我的Entity类中。 Is there an easy way (other than removing the column and the associated data from the exported dataset manually) of excluding that column from being (attempted to be) added in when I do INSERT? 是否有一种简便的方法(除了手动从导出的数据集中删除列和关联的数据之外),当我执行INSERT时排除(尝试)添加该列?

In hibernate every non static non transient property (field or method depending on the access type) of an entity is considered persistent, unless you annotate it as @Transient. 在休眠状态下,实体的每个非静态非瞬时属性(取决于访问类型的字段或方法)都被视为持久性的,除非将其注释为@Transient。

for example, 例如,

@Transient
public int counter; //transient property

private String firstname; //persistent property

The methods and fields annotated as @Transient will be ignored by the entity manager.See here for more information. 注释为@Transient的方法和字段将被实体管理器忽略。有关更多信息,请参见此处

Maybe this answer comes a little bit late, but I've just run into a similar problem and wrote the following method to solve it (I'm using dbUnit 2.5.0). 也许这个答案来得有点晚,但是我遇到了一个类似的问题,并编写了以下方法来解决它(我使用的是dbUnit 2.5.0)。 Hope it helps somebody. 希望它能帮助到别人。

/**
 * 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());
}

The core of the method is DefaultColumnFilter . 该方法的核心是DefaultColumnFilter I'm using a commodity static method here, but an instance of DefaultColumnFilter gives a lot of flexibility. 我在这里使用商品静态方法,但是DefaultColumnFilter的实例提供了很大的灵活性。

I wonder if there is a more straight forward way of doing this. 我想知道是否有更直接的方法来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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