DbUnit NoSuchTableException 与 Spring、Hibernate、HSQSLB

发布于 2024-12-20 10:12:05 字数 4331 浏览 1 评论 0原文

我正在为当地学区做一些志愿者发展。他们使用 Spring 和 Hibernate 来构建基本的 CRUD 应用程序。我能够针对开发数据库运行一些 JUnit 测试,但不想使用 DbUnit 针对 HSQLDB 进行尝试。

当 DbUnit 从 XML 文件加载初始数据时,我收到 NoSuchTableException 异常。我读过 StackOverflow 上有关此问题的其他帖子,他们倾向于建议将 Hibernate 属性 hibernate.hbm2ddl.auto 设置为 create-drop。我已经这样做了,但仍然遇到异常。我想知道我的设置中是否缺少一些基本的东西。

这是我的测试用例:

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

这是用于填充表的 DbUnit 数据:

<?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>

这是 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>

这个 spring-test-dao.xml 定义了 DAO 和 Hibernate 属性:

<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>

最后,这是该表的 Hibernate 文件:

<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>

请注意,我将 hibernate 属性 hbm2dll.auto 设置为“create-drop”。这就是其他 SO 帖子的建议。这应该在初始化期间创建表,但我得到以下堆栈跟踪:

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...

是否缺少一个简单的配置? Hibernate 文件中的模式和目录名称是否必须以某种方式与 hsqldb URL 匹配? 任何帮助表示赞赏!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

客…行舟 2024-12-27 10:12:05

您的单元测试是否启动了休眠框架。因为看起来您想使用 Hibernate 来创建表。但这仅在您的测试首先“启动”休眠时才有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文