2012-01-10 16 views
0

MS SQLServerからxmlファイルにデータをエクスポートし、データベースを必要とする単体テストを実行する際にそのデータセットを使用します。私はそれにdbunit mavenプラグインを使用します。dbunitを使用してINSERT中にHSQLDBに除外する方法

私にとって残念ながら、一部のテーブルのすべての列が自分のエンティティクラスにマッピングされているわけではありません。

例として、「member」というテーブルがあります。 メンバーテーブルには、memberid、membername、memberrankの3つのカラムがあります。 エクスポートを実行すると、3つの列すべてがエクスポートされます。 しかし、私のMemberEntityクラスでは、私のアプリケーションにmemberrankは必要ないので、私はmemberidとmembernameだけをマッピングします。だから私はMemberEntityはこのように見ているだろう:その後、私はテストケースの前にHSQLDBにデータセットを挿入しよう

@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; 
... 
} 

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を実行したときにその列が追加されないように(削除しようとしている)ことを排除する簡単な方法(エクスポートされたデータセットから列と関連するデータを手動で削除する以外)はありますか?

+0

問題が解決しましたか?私は、コメントや承認された答えを見ることができませんでした。 – ManuPK

答えて

1

休止状態で@Transientとしてアノテーションを付けない限り、エンティティの非静的で非一時的なプロパティ(フィールドまたはアクセスタイプに依存するメソッド)はすべて永続的なものとみなされます。例えば

@Transient 
public int counter; //transient property 

private String firstname; //persistent property 

@Transientと注釈メソッドとフィールドを詳細についてmanager.See hereエンティティによって無視されます。

+1

これは私が正確に探していたものではありません。私は、Entityクラスを変更せずに、DBUnitに特定の列を無視させる方法をとっていました。とにかく私のエンティティクラスのそれらの列を追加してしまった。 –

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というインスタンスは柔軟性があります。

これを実行するより簡単な方法があるのだろうかと思います。

関連する問題