我是嵌入式数据库的新手,但至少运行了它。令我困惑的是我的数据并没有在运行之间保存。我的意思是说这不是很好的测试?我不想将数据添加到我的数据库,我每次运行Spring + Hibernate + H2嵌入式数据库。数据如何保存?
所以我寻找一种方式来做到这一点的应用时间,我发现我会配置哪些我想这样
props.put("hibernate.connection.url", "jdbc:h2:~/test");
休眠连接网址
在我的HibernateConfiguration.java中。没有成功虽然没有错误,但也没有保存,我没有找到该测试文件,应该从该网址创建。 (运行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>
,并执行脚本每次运行应用程序时,但事情是,我想Hibernate来处理所有的表格创建等。
这是如何正常完成的?
我搜索了几个小时,但还没有得到它。
Ps。如果需要,我会发布我所有的配置。
编辑: 更新我的问题,以包含关注一个问题,并包括我的配置。
HibernateConfiguration.java package 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());
}
}
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>
当然还有一个pom,我得到所有的依赖关系,但我不认为这是必要的。
我创建一个对象(Course.java)并将其保存到数据库,这很好,我可以再次加载它。但是,当我更改我的代码并重新加载应用程序时,该对象不再存在。
编辑2:我正在添加这样的数据。
绑定会话工厂。
@Autowired
private SessionFactory sessionFactory;
将这样的课程对象添加到数据库中。
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();
这工作正常,我可以得到该课程。 但是,当我下次运行应用程序时,IDA511没有课程,我得到一个空指针异常。这是否意味着课程只能在课程中保存? hum
您是否将Hibernate hbm2ddl设置设置为创建 - 删除?如果是这样,一旦连接关闭,您的数据将被丢弃。查看http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html表3.7 ...还有关于引导SQL,放置一个名为** import的文件。 sql **在你的classpath根目录下,而hibernate会运行它。看到这篇文章:http://stackoverflow.com/questions/673802/how-to-import-initial-data-to-database-with-hibernate – gerrytan 2013-02-19 01:26:34
这似乎是关于不同事情的几个问题。最好把它缩小到一个问题,给出更多关于这个问题的细节,并为其他问题打开其他问题。 – 2013-02-19 02:49:31
你可以发布所有配置文件(至少休眠的)? – 2013-02-19 05:26:01