안녕하세요, JPA 및 단위 테스트를 사용하여 DB 스키마가 만들어 졌는지 확인하기 위해 약간의 POC를 시도하고 있습니다. 나는 H2 DB와 함께 일하고 있는데 엔티티에서 스키마를 생성하기 위해 Hibernate를 설정했지만, DbUnit이 데이터 셋에서 DB를 초기화하려고 할 때 나는 항상 테이블을 얻는다. tableMap에서 찾을 수 없다. DB URL에 속성 DB_CLOSE_DELAY = -1을 추가해야한다는 것을 알았지 만, DbUnit이 초기화를 시도 할 때 Hibernate가 스키마를 생성 한 후에 DB가 손실 된 것과 같습니다.DbUnit H2 봄 및 최대 절전 모드 메모리 데이터베이스
아이디어가 있으십니까? 어떤 도움을 주셔서 감사합니다.
응용 프로그램의 context.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;
}
}
데이터 세트-systementity.xml
,536,913 :이
내 설정이다 63,210<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<System_Entities id="2" name="NAME" phone01="+52-55-55555555" email="[email protected]"
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]
로그가 오류없이 모든 SQL 문장을 보여주기 때문에 나는 테이블이 최대 절전 모드에 의해 생성되는 것을 볼 수 있습니다.
감사합니다.
솔루션
감사합니다 마크 로빈슨 I가 설정 방법을 수정 : 그것은 지금 작동
@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를 사용하는 경우 내가 가지고 있지 않은 것입니다 이 문제.
와 같은 간단한 쿼리를 실행하여 강제로 적용 할 수도 있습니다. 감사합니다! 문제는 지금 내 테스트 메서드에서 저장소 개체를 사용하여 개체를 저장하고 그 후에 dbUnitCon에서 createDataSet()을 사용하여 실제 데이터 세트를 가져 오려고하지만 "테이블을 잠그려고 시도하는 시간 초과 ..."오류가 발생합니다. 연결 URL에 MVCC = TRUE를 추가하면 작동하지만 dataset에는 dbunit가 초기화하는 데이터 만 포함되며 리포지토리 개체와 함께 저장된 개체는 포함되지 않습니다. – Arless
이전 메모를 계속하면 저장소에서 사용하는 트랜잭션이 아직 커밋되지 않은 것처럼 보이며 실제 데이터 집합을 가져 오려고하면 그 변경 내용을 볼 수 없으며 이후에 어디로 이동해야하는지 알 수 없습니다. – Arless