Im new to embedded databases but I got it running at least. What confuses me is that my data is not saved between runs. I mean wouldn't that be nice for testing? I don't wanna add data to my database every time i run the application
So I searched for a way to do this and I found that I shall configure a hibernate connection URL which I tried like this
props.put("hibernate.connection.url", "jdbc:h2:~/test");
in my HibernateConfiguration.java. Without success though, no errors but also nothing saved, and I did not find that test file that should be created from that URL. (Running windows and checked my user folder)
I also saw that its possible to do like this
<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>
And execute the scripts every time i run the application, but the thing is that I want hibernate to handle all the creating of tables etc.
How is this normally done?
I searched for some hours now but haven't still got it.
Ps. if it's needed i'll post all my configs.
Edit: Updated my question to contain focus on one question and included my configs.
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 where i only added the embedded database tag.
<?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>
There of course also a pom where i got all dependencies but i don't think that is necessary.
I'm creating a object (Course.java) and saving that to the db, that's fine and i can load it again. But when I change my code and the application is reloaded the object doesn't exist anymore.
Edit2: I'm adding data like this.
Binding a session factory.
@Autowired
private SessionFactory sessionFactory;
Adding a my Course object to the database like this.
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();
This works fine and i can get that course. However, when I run the application next time there is no course with id IDA511 and i getting a null pointer exception. Does this mean that the course is only saved in the session maybe? hum