5

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

6
  • Did you set Hibernate hbm2ddl settings to create-drop? If so your data will be dropped once the connection is closed. Have a look at docs.jboss.org/hibernate/orm/3.3/reference/en/html/… Table 3.7... Also about the bootstrap SQL, place a file called import.sql on your classpath root and hibernate will run it. See this post: stackoverflow.com/questions/673802/… Commented Feb 19, 2013 at 1:26
  • 1
    That seems like several questions about different things. It would be better to narrow it down to one problem, give more details about that one, and open other questions for the others. Commented Feb 19, 2013 at 2:49
  • Could you post all configuration files (at least the Hibernate ones)? Commented Feb 19, 2013 at 5:26
  • Hm, I don't know what the problem could be. There should be a file called test.h2.db in your system somewhere... In the current user home directory actually, but maybe the application is running as another user (not sure). Could you try to find this file? Commented Feb 19, 2013 at 10:36
  • Thanks but I can't find that file. Searched the computer for it and browsed for it also. I updated my question with how I adding data also. Commented Feb 19, 2013 at 10:57

2 Answers 2

0

Since you are running your application on Windows, perhaps the home directory is not found using the tilde '~' operator.

Try giving your hibernate.connection.url property an absolute path eg. "C:\test" and check in that folder when you run the application to see if H2 creates a file for you.

Sign up to request clarification or add additional context in comments.

Comments

0

For saving h2 in user directory as a file you can use

jdbc:h2:file:~/rmdb;AUTO_SERVER=TRUE;

and if you application not running, you can connect to it for example from intellij IDEA db util.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.