I am new to embedded databases, but I got it at least. What confuses me is that my data is not saved between runs. I mean, that would be nice for testing? I do not want to add data to my database every time I run the application
So, I was looking for a way to do this, and I found that I was setting up the sleep mode URL that I tried, like this
props.put("hibernate.connection.url", "jdbc:h2:~/test");
in my HibernateConfiguration.java. Without success, however, no errors, but nothing was saved, and I did not find this test file, which should be created from this URL. (Launch windows and check user folder)
I also saw that this can be done 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 fact is that I want hibernate to handle all table creation, etc.
How is this usually done?
I searched for a few hours but have not received it yet.
Ps. if necessary, I will publish all my configs.
Edit: Updated my question to focus on one question and enable 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 added only the inline 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"> <annotation-driven /> <resources mapping="/resources/**" location="/resources/" /> <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 where I got all the addictions, but I donβt think it is necessary.
I create an object (Course.java) and save it in db, this is great and I can load it again. But when I change my code and the application restarts, the object no longer exists.
Edit2 . I am adding such data.
Factory session binding.
@Autowired private SessionFactory sessionFactory;
Adding a course object to a 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 great and I can get this course. However, the next time I launch the application, there is no course with IDA511 and I get a null pointer exception. Does this mean that the course can only be saved in the session? hum