Spring + Hibernate + H2嵌入式数据库.如何保存数据?

nil*_*lsi 5 spring hibernate spring-mvc h2 embedded-database

我是嵌入式数据库的新手,但我至少运行它.令我困惑的是我的数据没有在运行之间保存.我的意思是测试不是很好吗?每次运行应用程序时,我都不想将数据添加到我的数据库中

所以我搜索了一种方法来做到这一点,我发现我将配置一个hibernate连接URL,我尝试这样做

props.put("hibernate.connection.url", "jdbc:h2:~/test");
Run Code Online (Sandbox Code Playgroud)

在我的HibernateConfiguration.java中.虽然没有成功,但没有错误但也没有保存,我没有找到应该从该URL创建的测试文件.(运行Windows并检查我的用户文件夹)

我也看到它可以像这样做

<jdbc:embedded-database id="dataSource" type="H2">
    <jdbc:script location="classpath:db-schema.sql"/>
    <jdbc:script location="classpath:db-test-data.sql"/>
</jdbc:embedded-database>
Run Code Online (Sandbox Code Playgroud)

并且每次运行应用程序时都执行脚本,但问题是我希望hibernate处理所有表的创建等.

这通常是怎么做的?

我现在搜索了几个小时,但还没有得到它.

PS.如果需要我会发布我的所有配置.

编辑: 更新了我的问题以包含对一个问题的关注并包括我的配置.

HibernateConfiguration.java包com.courseinfo.project;

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.dialect.H2Dialect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;

import com.courseinfo.project.model.Course;

@Configuration
public class HibernateConfiguration {

    @Value("#{dataSource}")
    private DataSource dataSource;

    @Bean
    public AnnotationSessionFactoryBean sessionFactoryBean() {
        Properties props = new Properties();
        props.put("hibernate.dialect", H2Dialect.class.getName());
        props.put("hibernate.format_sql", "true");
        props.put("hibernate.connection.url", "jdbc:h2:~/test");

        AnnotationSessionFactoryBean bean = new AnnotationSessionFactoryBean();
        bean.setAnnotatedClasses(new Class[]{Course.class});        
        bean.setHibernateProperties(props);
        bean.setDataSource(this.dataSource);
        bean.setSchemaUpdate(true);
        return bean;
    }

    @Bean
    public HibernateTransactionManager transactionManager() {
        return new HibernateTransactionManager( sessionFactoryBean().getObject() );
    }

}
Run Code Online (Sandbox Code Playgroud)

servlet-context.xml其中我只添加了嵌入式数据库标记.

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jdbc
        http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"
        default-lazy-init="true">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.courseinfo.project" />


    <jdbc:embedded-database id="dataSource" type="H2"/>


</beans:beans>
Run Code Online (Sandbox Code Playgroud)

当然还有一个pom,我得到了所有的依赖,但我不认为这是必要的.

我正在创建一个对象(Course.java)并将其保存到db,这很好,我可以再次加载它.但是当我更改我的代码并重新加载应用程序时,该对象不再存在.

Edit2:我正在添加这样的数据.

绑定会话工厂.

@Autowired
private SessionFactory sessionFactory;
Run Code Online (Sandbox Code Playgroud)

像这样将我的Course对象添加到数据库中.

Course course = new Course();
course.setCourseId("IDA512");
Session s = sessionFactory.openSession();
s.saveOrUpdate(course);
s.flush();
s.clear();
Course course2 = (Course) s.get(Course.class, "IDA511");
s.close();
Run Code Online (Sandbox Code Playgroud)

这很好,我可以得到那个课程.但是,当我下次运行应用程序时,没有id IDA511的课程,我得到一个空指针异常.这是否意味着课程只保存在会话中?哼

小智 0

由于您在 Windows 上运行应用程序,因此可能无法使用波形符“~”运算符找到主目录。

尝试为您的 hibernate.connection.url 属性提供绝对路径,例如。“C:\test”,并在运行应用程序时检查该文件夹,看看 H2 是否为您创建了一个文件。