我正在尝试测试一个使用 SpringMVC 和 Hibernate 与 MySQL 的应用程序。我尝试使用 HSQLDB,但由于语法与 MySQL 不同,查询可能无法正常工作,所以我决定使用 H2。 问题是,当我运行它时,它说“未找到表 MAILCONFIG”,如果我在 INIT 语法上创建它,它会说它已经存在。
我配置了一个不执行任何操作的简单测试,我只想运行它。
我有以下文件:
ServiceTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/test/resources/applicationContext.xml"})
@Transactional
@WebAppConfiguration
public class ServiceTest {
private static DataSource ds;
@BeforeClass
public static void setUpConnection(){
ds = new DataSource();
ds.setDriverClassName("org.h2.Driver");
ds.setUrl("jdbc:h2:mem:testDB");
ds.setUsername("sa");
ds.setPassword("");
HibernateConfiguration.dataSourceTest(ds);
}
@AfterClass
public static void cleanConnection(){
HibernateConfiguration.dataSourceTest(null);
}
}
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:persistence.xml" />
<property name="persistenceUnitName" value="testingSetup" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
</bean>
<context:component-scan base-package="com.adistec" />
<context:annotation-config/>
持久性.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="testingSetup" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="org.h2.Driver" />
<property name="hibernate.connection.url" value="jdbc:h2:mem:testDB;DB_CLOSE_DELAY=-1;MODE=MySQL" />
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
MailConfig.java
@Entity
public class MailConfig {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@Column
String host;
}
我在测试中指定了 Hibernate 配置,因为应用程序使用 java 类而不是 XML 文件。
有人可以帮我解决这个问题吗?或者,如果有一种更简单的方法来使用 java 类进行测试也是受欢迎的,但我还找不到解决方案。 我希望它能够在本地工作,然后它也必须与 jenkins 一起工作,这样它就可以创建在虚拟机上无法完成的事情。
谢谢!
最佳答案
您的 MailConfig 类中似乎缺少 @Table
注释。
您可以从 http://mycuteblog.com/h2-database-example-hibernate-spring-boot/ 找到使用 spring、hibernate 和 h2 数据库的完整工作示例
关于java - Hibernate 未在内存数据库中使用 H2 创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40635400/