【发布时间】:2013-02-03 13:15:36
【问题描述】:
我是嵌入式数据库的新手,但我至少可以运行它。让我感到困惑的是,我的数据在运行之间没有保存。我的意思是这不是很好的测试吗?我不想每次运行应用程序时都将数据添加到我的数据库中
所以我搜索了一种方法来做到这一点,我发现我应该配置一个我尝试过的休眠连接 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 的课程并且我得到一个空指针异常。这是否意味着课程可能只保存在会话中?哼
【问题讨论】:
-
您是否将 Hibernate hbm2ddl 设置设置为 create-drop?如果是这样,一旦连接关闭,您的数据将被删除。看看docs.jboss.org/hibernate/orm/3.3/reference/en/html/… 表 3.7... 关于引导 SQL,在你的类路径根目录下放置一个名为 import.sql 的文件,hibernate 将运行它。看到这个帖子:stackoverflow.com/questions/673802/…
-
这似乎是关于不同事物的几个问题。最好将其缩小到一个问题,提供有关该问题的更多详细信息,并为其他问题打开其他问题。
-
您能发布所有配置文件(至少是 Hibernate 的)吗?
-
嗯,我不知道可能是什么问题。您的系统中应该有一个名为
test.h2.db的文件...实际上在当前用户主目录中,但可能应用程序正在以另一个用户身份运行(不确定)。你能试着找到这个文件吗? -
谢谢,但我找不到那个文件。在计算机上搜索并浏览了它。我还更新了我如何添加数据的问题。
标签: spring hibernate spring-mvc h2 embedded-database