我是嵌入式数据库的新手,但我至少让它运行起来了。让我感到困惑的是,我的数据在运行之间没有保存。我的意思是这不是很好的测试吗?我不想每次运行应用程序时都将数据添加到我的数据库中
所以我搜索了一种方法来做到这一点,我发现我应该配置一个我尝试过的休眠连接 URL
props.put("hibernate.connection.url", "jdbc:h2:~/test");
在我的 HibernateConfiguration.java 中。但是没有成功,没有错误,但也没有保存任何内容,而且我没有找到应该从该 URL 创建的测试文件。 (运行窗口并检查我的用户文件夹)
我也看到它可以这样做
<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 处理所有表的创建等。
这通常是如何完成的?
我现在搜索了几个小时,但仍然没有得到它。
附言。如果需要,我会发布我所有的配置。
编辑: 更新了我的问题以包含对一个问题的关注并包含我的配置。
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() );
}
}
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)并将其保存到数据库中,这很好,我可以再次加载它。但是当我更改我的代码并重新加载应用程序时,该对象不再存在。
Edit2:我正在添加这样的数据。
绑定会话工厂。
@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();
这很好用,我可以上那门课。但是,当我下次运行应用程序时,没有 ID 为 IDA511 的课程并且我得到一个空指针异常。这是否意味着课程可能只保存在会话中?哼
由于您在 Windows 上运行应用程序,因此使用波浪号“~”运算符可能找不到主目录。
尝试给你的 hibernate.connection.url 属性一个绝对路径,例如。 “C:test”并在您运行应用程序时检查该文件夹以查看 H2 是否为您创建文件。