gpt4 book ai didi

spring - 带有 Spring 和 Hibernate 的内存数据库中的 DbUnit H2

转载 作者:行者123 更新时间:2023-12-01 01:06:48 25 4
gpt4 key购买 nike

嗨,我正在尝试使用 JPA 和单元测试进行一些 POC,以验证数据库架构是否已创建。我正在使用 H2 DB 并且我设置为 Hibernate 从实体创建模式,但是当 DbUnit 尝试从数据集初始化 DB 时,我总是得到一个表......在 tableMap 中找不到。我读到我必须将属性 DB_CLOSE_DELAY=-1 添加到 DB URL,但是就像在 Hibernate 创建架构之后,当 DbUnit 尝试初始化时,DB 丢失了。

有任何想法吗?任何帮助都受到高度赞赏。

这是我的配置:

应用程序上下文.xml

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSourceH2" />
<property name="packagesToScan" value="com.xxx.model" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="showSql" value="true" />
<!-- property name="databasePlatform" value="org.hibernate.dialect.MySQLInnoDBDialect" /-->
<property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" />
<!-- property name="database" value="MYSQL" /-->
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="javax.persistence.validation.mode">CALLBACK</prop>
</props>
</property>
</bean>

<bean id="dataSourceH2"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>

RepositoryTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/application-context-test.xml" })
@Transactional
public class SystemEntityRepositoryH2Test {

@Inject
private SystemEntityRepository repository;

@Inject
private DataSource dataSourceH2;

private IDatabaseConnection connection;

@Before
public void setUp() throws Exception {
IDatabaseConnection dbUnitCon = null;
dbUnitCon = new DatabaseDataSourceConnection(dataSourceH2, "testdb");
dbUnitCon.getConfig().setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);

IDataSet dataSet = this.getDataSet("dataset-systementity.xml");
DatabaseOperation.INSERT.execute(dbUnitCon, dataSet);

}

@After
public void tearDown() throws Exception {
//DatabaseOperation.DELETE_ALL.execute(this.getConnection(), this.getDataSet(dataSetFile));
}

@Test
public void test() throws Exception {

}

protected IDataSet getDataSet(String dataSetFile) throws Exception {
ResourceLoader resourceLoader = new ClassRelativeResourceLoader(this.getClass());
Resource resource = resourceLoader.getResource(dataSetFile);

if (resource.exists()) {
return new FlatXmlDataSetBuilder().build(resource.getInputStream());
}

return null;
}
}

数据集系统实体.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<System_Entities id="2" name="NAME" phone01="+52-55-55555555" email="a@a.com"
address01="Street" address02="123" address03="1" address04="Address04"
address05="Address05" city="City" state="State" country="MX"
zipcode="12345" />
</dataset>

错误
ERROR DatabaseDataSet:286 - Table 'System_Entities' not found in tableMap=org.dbunit.dataset.OrderedTableNameMap[_tableNames=[], _tableMap={}, _caseSensitiveTableNames=false]

我可以看到这些表是由hibernate创建的,因为日志显示了所有的sql语句没有错误。

谢谢。

解决方案

感谢马克罗宾逊
我将 setUp 方法修改为:
@Before
public void setUp() throws Exception {
IDatabaseConnection dbUnitCon = null;
EntityManager entityManager = entityManagerFactory.createEntityManager();
Session session = entityManager.unwrap(Session.class);
SessionImplementor si = (SessionImplementor) session;
Connection conn = si.getJdbcConnectionAccess().obtainConnection();
dbUnitCon = new DatabaseConnection(conn);

//dbUnitCon.getConfig().setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);

IDataSet dataSet = this.getDataSet("dataset-systementity.xml");
DatabaseOperation.INSERT.execute(dbUnitCon, dataSet);
}

它现在可以工作了,我还不明白的是,如果我使用 HSQLDB,我就没有这个问题。

最佳答案

问题是 DBUnit 在 Hibernate 可以初始化之前加载表数据。

作为您的 @setup 的一部分,您需要获取 Hibernate session 。这应该会导致 Hibernate 创建您的表。你甚至可以通过执行像 select 1 这样的简单查询来强制它。

关于spring - 带有 Spring 和 Hibernate 的内存数据库中的 DbUnit H2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18153363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com