2 Replies Latest reply on May 19, 2006 8:29 AM by mmarcom

    EJB3 Table creation / dbunit problems

    mmarcom

      hello all,
      i am testing a simple EJB3 application using DBUnit.
      In my app, i have an Expense EEJB which has a One-To-Many relationship with an Item EEJB.
      i am trying to test using dbunit to populate/erase data before or after each test.. and i am getting a catastrophic :) exception from dbunit

      org.dbuniing dbunit..
      Errort.dataset.NoPrimaryKeyException: EXPENSE_ITEM

      by doing some debug i found out that the sql code created by jboss (microcontainer) for my EEJBs is the following


      create table expense (id integer generated by default as identity (start with 1), site varchar(255), date date, primary key (id))
      2006-05-18 10:39:30,234 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport [main] - create table expense_item (expense_id integer not null, items_id integer not null, unique (items_id))
      2006-05-18 10:39:30,250 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport [main] - create table item (id integer generated by default as identity (start with 1), price double not null, name varchar(255), primary key (id))
      2006-05-18 10:39:30,265 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport [main] -

      so this is the sql code for creating the expense_item table

      create table expense_item (expense_id integer not null, items_id integer not null, unique (items_id))

      there's no primary key, so dbunit will fail to delete the table

      am i using the wrong relationship?

      anyone could help?

      thx and regards
      marco


      PS here's my dbunit code

      @Configuration(groups = "integration.ejb3", afterTestMethod = true)
       public void clearDatabase() throws Exception {
       IDatabaseConnection dbUnitConn = null;
       try {
       DataSource defaultDs = (DataSource)EJB3ContainerTestNG.lookup("java:/DefaultDS");
       System.err.println("Datasource is not null..");
       System.err.println("INvoking dbunit..");
       dbUnitConn = new DatabaseDataSourceConnection(defaultDs);
      
       ITableFilter filter = new DatabaseSequenceFilter(dbUnitConn);
       IDataSet dataset = new FilteredDataSet(filter,
       dbUnitConn.createDataSet());
      
      
       DatabaseOperation.DELETE.execute(dbUnitConn, dataset);
       } catch(Exception e) {
       System.err.println("Error in deleting dtabase..");
       e.printStackTrace();
       } finally {
       if (dbUnitConn != null)
       dbUnitConn.close();
       }
      
       System.err.println("DBunit invoked..");
       }
      i attach at the end of msg the dbunit code that i am using, since