-4

I want to use H2 database filesystem in my Java project. But I don't know how to implement by using properties file in java.

  • There are plenty of online tutorials on this. Can you please use Google? – Prashant Jun 27 '18 at 04:35
  • I went through Google but I didn't find how to use H2 database filesystem. If I choose password and username. Then it doesn't open connection. – user9265872 Jun 27 '18 at 04:44
  • Surprising! Can you please post your code with the exception you are getting. In that way it would be easier for the community to help you. – Prashant Jun 27 '18 at 04:51
  • H2 is a [database-management system](https://en.wikipedia.org/wiki/Database), not a [file system](https://en.wikipedia.org/wiki/File_system). Please do some basic research, reading, and study before posting. And when posting on Stack Overflow, ask only narrowly-focused questions on a very specific technical issue. For more open-ended discussion, seek other sites such as http://www.JavaRanch.com/. – Basil Bourque Jun 27 '18 at 05:31
  • Always search Stack Overflow thoroughly before posting. You could have many existing Answers, such as [this one by Alex Miller](https://stackoverflow.com/a/155423/642706). [I myself have posted multiple complete working examples of connecting to H2 and using it.](https://duckduckgo.com/?q=site%3Astackoverflow.com+basil+H2+example&t=osx&ia=web) such as [this](https://stackoverflow.com/a/50780237/642706) and [this](https://stackoverflow.com/a/42919500/642706) and [this](https://stackoverflow.com/q/46839689/642706) and [this](https://stackoverflow.com/a/43039615/642706). – Basil Bourque Jun 27 '18 at 05:46

1 Answers1

0

There is some code that you can refer to. I am currently using this and this is working like a charm:

package group.demo.custMgmt.common;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:group/demo/custMgmt/resources/persistence-sql.properties" })
@ComponentScan({ "group.*" })
public class HibernateConfig {

    @Autowired
    private Environment env;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(restDataSource());
        sessionFactory.setPackagesToScan("*");
        sessionFactory.setHibernateProperties(hibernateProperties());
        sessionFactory.setMappingResources("Model.hbm.xml");
        return sessionFactory;
    }

    @Bean(destroyMethod="close")
    public DataSource restDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        dataSource.setUrl(env.getProperty("jdbc.url"));
        dataSource.setUsername(env.getProperty("jdbc.username"));
        dataSource.setPassword(env.getProperty("jdbc.password"));
        return dataSource;
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {

        HibernateTransactionManager txManager = new HibernateTransactionManager();
        txManager.setSessionFactory(sessionFactory);

        return txManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    Properties hibernateProperties() {
        return new Properties() {
            /**
             * 
             */
            private static final long serialVersionUID = -7570975785205994647L;

            {
                setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
                setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
                setProperty("hibernate.globally_quoted_identifiers", "true");
                setProperty("hibernate.show_sql",env.getProperty("hibernate.show_sql"));
            }
        };
    }
}

persistence-sql.properties

`spring.h2.console.enabled=true
jdbc.driverClassName =org.h2.Driver
jdbc.url =jdbc:h2:tcp://localhost:9092/~/test
jdbc.username =sa
jdbc.password =
hibernate.dialect =org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=create
hibernate.show_sql=true`

maven dependency

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.196</version>
    <scope>runtime</scope>
</dependency>
Sagar Kharab
  • 369
  • 2
  • 18