简体   繁体   中英

Hibernate is not creating tables with H2 in memory database

I'm trying to test an application which uses SpringMVC and Hibernate with MySQL. I tried using HSQLDB but since the syntax is not the same as MySQL the queries may not work, so I decided to go to H2. The problem is that when I run it it says "Table MAILCONFIG not found", and if I created on the INIT syntax it says that already exists.

I have configured a simple test that doesn't do anything, I only want it to run.

I have the following files:

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

persistence.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;
}

I specify the Hibernate configuration in the test because the application uses java classes and not the XML files.

Can anyone help me with this? Or if there is an easier way to test it with java classes is welcome too, I couldn't find out a solution yet. I want it to work locally and then it has to work with jenkins too, so it can create things that can't do on the VM.

Thank you!

Seems @Table annotation is missing in your MailConfig class.

You can find full working example using spring, hibernate and h2 database from http://mycuteblog.com/h2-database-example-hibernate-spring-boot/

在主类中添加 @EntityScan(basepackage="com.entity") 注解

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