Im 임베디드 데이터베이스에 새로 추가되었지만 적어도 실행 중이어야합니다. 나 혼란 스러울 때 데이터가 실행 사이에 저장되지 않는다는 것입니다. 그게 시험하기에 좋지 않겠 니? 나는 싶어 내 데이터베이스에 내가 봄 + 최대 절전 모드 + H2 임베디드 DB. 데이터는 어떻게 저장됩니까?
그래서 내가 할 수있는 방법을 검색 응용 프로그램을 실행할 때마다 데이터를 추가하지 않고 나는 내가이props.put("hibernate.connection.url", "jdbc:h2:~/test");
같은 시도한 최대 절전 모드 연결 URL을 구성해야 발견 내 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>
좋아해요 그리고 스크립트에게 내가 응용 프로그램을 실행할 때마다 실행하는 것을보고
하지만 일은 I가 최대 절전 모드로 할 것입니다 모든 테이블 만들기 등을 처리하십시오.
어떻게 정상적으로 수행됩니까?
나는 지금 몇 시간 동안 수색했지만 아직 얻지 못했습니다.
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)를 만들고 db에 저장하면 괜찮습니다. 다시로드 할 수 있습니다. 그러나 코드를 변경하고 응용 프로그램이 다시로드되면 객체가 더 이상 존재하지 않습니다.
편집 2 : 다음과 같은 데이터가 추가됩니다.
세션 팩토리를 바인드합니다.
@Autowired
private SessionFactory sessionFactory;
이와 같이 데이터베이스에 내 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();
이 작업은 정상적으로 진행되며 그 과정을 수강 할 수 있습니다. 그러나 다음에 응용 프로그램을 실행할 때 IDA511 ID가없는 코스가 있으며 널 포인터 예외가 발생합니다. 이것은 코스가 세션에서만 저장된다는 것을 의미합니까? 험
Hibernate hbm2ddl 설정을 create-drop으로 설정 했습니까? 연결이 끊어지면 데이터가 삭제됩니다. http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html 표 3.7 ... 부트 스트랩 SQL에 대해서도 ** import라는 파일을 배치하십시오. 클래스 경로 루트에 sql ** 및 최대 절전 모드가 실행됩니다. 이 게시물을 참조하십시오 : http://stackoverflow.com/questions/673802/how-to-import-initial-data-to-database-with-hibernate – gerrytan
다른 것들에 대한 몇 가지 질문처럼 보입니다. 하나의 문제로 좁히고 그 문제에 대해 더 자세히 설명하고 다른 질문을 열면 더 낫습니다. –
모든 구성 파일 (최소한 Hibernate 파일)을 게시 할 수 있습니까? –