3

I'm getting back into the Java world, and I'm trying to configure a new Spring web application with JPA, Hibernate and PostgreSQL.

I have found a lot of older examples with various XML configuration files, and I'm wondering if there is a preferred new way to perform this configuration without relying on XML file authoring.

Some of the things I need to configure are the hibernate sql dialect, driver, etc.

BlakeH
  • 3,354
  • 2
  • 21
  • 31

2 Answers2

7

Put the following fragments into a class annotated with @Configuration and @EnableTransactionManagement

Hibernate/JPA (edit the packagesToScan String):

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan(new String[] { "com.XY.model" });
    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(additionalProperties());
    return em;
}

Properties additionalProperties() {
    Properties properties = new Properties();
    properties.setProperty("hibernate.hbm2ddl.auto", "update");
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");
    properties.setProperty("hibernate.show_sql", "true");
    return properties;
}

DataSource (edit username, password and host address):

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("org.postgresql.Driver");
    dataSource.setUrl("jdbc:postgresql://localhost:port/DB_NAME");
    dataSource.setUsername("root");
    dataSource.setPassword("");
    return dataSource;
}

Transaction Manager:

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(emf);
    return transactionManager;
}
Lukehey
  • 1,284
  • 1
  • 11
  • 20
  • Is the DataSource class imported from javax.sql? – BlakeH Jan 06 '16 at 15:40
  • Yes, it is javax.sql.DataSource. The implementation is org.springframework.jdbc.datasource.DriverManagerDataSource – Lukehey Jan 06 '16 at 15:41
  • Could you please guide on this issue ASAP? http://stackoverflow.com/questions/35685804/how-to-make-database-configuration-configurable-of-persistance-xml-file-through/35686103#35686103 – PAA Feb 28 '16 at 18:10
2

If you're gonna use spring, i recommend to use Spring Boot which offers many auto configurations. you can use a application.properties for configuring dialects and stuff:

spring.datasource.url = <jdbcurl>
spring.datasource.username = <username>
spring.datasource.password = <password>
spring.datasource.driver-class-name = org.postgresql.Driver

Spring Boot provides a number of Starter Packages that make easy to add jars to your classpath. These Starter Packages simply provide dependencies that you are likely to need when developing a specific type of application. Since you're developing a possibly web application that requires data access, you should add these to your pom.xml:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
</parent>

<properties>
        <java.version>1.8</java.version>
</properties>

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
</dependencies>

Basically, spring boot tries to guess how you will want to configure your application, based on the jar dependencies that you have added. spring-boot-starter-data-jpa, provides the following key dependencies:

  • Hibernate — One of the most popular JPA implementations.
  • Spring Data JPA — Makes it easy to implement JPA-based repositories.
  • Spring ORMs — Core ORM support from the Spring Framework.

You can explicitly configure JPA settings using spring.jpa.* properties. For example, to create and drop tables you can add the following to your application.properties:

spring.jpa.hibernate.ddl-auto=create-drop

You can read more about spring boot here

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
  • Thanks for the exhaustive response. Is application.properties a configuration file? – BlakeH Jan 06 '16 at 15:38
  • Yeap, spring boot allows you to configure your apps in many ways, on of them is the `application.properties`. You can also use YAML files, environment variables and command-line arguments to configure your app. – Ali Dehghani Jan 06 '16 at 15:42
  • 1
    Thanks again for your answer, I wish I could mark both as accepted. I'm going to use Lukehey's answer for my current project and I may utilize your method on the next project to compare. – BlakeH Jan 06 '16 at 16:42