简体   繁体   中英

DbUnit NoSuchTableException with Spring, Hibernate, HSQSLB

I'm doing some volunteer development for the local school district. They use Spring and Hibernate for a basic CRUD application. I was able to get some JUnit tests running against a development database, but would not like to try it against HSQLDB using DbUnit.

I'm getting a NoSuchTableException with DbUnit loading initial data from an XML file. I've read the other posts on StackOverflow regarding this, and they tend to recommend setting Hibernate property hibernate.hbm2ddl.auto to create-drop. I've done this and still get the exception. I'm wondering if there is something basic in the setup that I'm missing.

This is my test case:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath:spring-test-datasource.xml",
        "classpath:spring-test-dao.xml"
})
@Transactional
public class HibernateRitsAdminDaoTest {
    @Autowired protected RitsAdminDao ritsAdminDao;
    @Autowired protected DataSource dataSource;
    protected IDatabaseTester dbTester;

    @Before
    public void setUp() throws Exception {
        dbTester = new DataSourceDatabaseTester(dataSource);
        IDataSet dataSet = new FlatXmlDataSetBuilder().build(new FileInputStream("test/RitsAdminsTest.xml"));
        dbTester.setDataSet(dataSet);
        dbTester.onSetup();
    }

    @After
    public void tearDown() throws Exception {
        dbTester.onTearDown();
    }

    @Test
    public void testGetAll() throws Exception {
        List<RitsAdmin> ritsAdmins = ritsAdminDao.getAll();
        assertTrue(ritsAdmins.size() > 0);
    }
}

This is my DbUnit data for to populate the table:

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
    <tbl_sn_rits_admins profile_id="1"/>
    <tbl_sn_rits_admins profile_id="6"/>
    <tbl_sn_rits_admins profile_id="88"/>
</dataset>

This is the datasource definition in spring-test-datasource.xml

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
        <property name="url" value="jdbc:hsqldb:mem:schoolnet"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    </bean>

And this spring-test-dao.xml that has the DAO and Hibernate properties defined:

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingResources">
            <list>
                <value>us/mn/k12/district/domain/RitsAdmin.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
            </props>
        </property>
    </bean>

    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

And lastly, here is the Hibernate file for the table:

<hibernate-mapping package="us.mn.k12.district.domain">
    <class name="RitsAdmin" table="tbl_sn_rits_admins" schema="dbo" catalog="schoolnet">
        <id name="profileId" type="long">
            <column name="profile_id" />
        </id>
    </class>
</hibernate-mapping>

Note that I set the hibernate property hbm2dll.auto to "create-drop". This is what the other SO posts suggest. This should create the table during initialization, but I'm getting the following stack trace:

org.dbunit.dataset.NoSuchTableException: tbl_sn_rits_admins
    at org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:288)
    at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:109)
    at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
    at org.dbunit.AbstractDatabaseTester.executeOperation(AbstractDatabaseTester.java:190)
    at org.dbunit.AbstractDatabaseTester.onSetup(AbstractDatabaseTester.java:103)
    at us.mn.k12.district.dao.HibernateRitsAdminDaoTest.setUp(HibernateRitsAdminDaoTest.java:41)
... keeps going...

Is there a simple configuration I'm missing? Do the schema and catalog names in the Hibernate file have to somehow match up with the hsqldb URL? Any help appreciated!

Did your unit test start the hibernate framework. Because it looks like you want to use Hibernate to create the tables. But this only works if your test "starts" hibernate first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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