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.

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Is the DataSource class imported from javax.sql?
Yes, it is javax.sql.DataSource. The implementation is org.springframework.jdbc.datasource.DriverManagerDataSource
Could you please guide on this issue ASAP? stackoverflow.com/questions/35685804/…
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

3 Comments

Thanks for the exhaustive response. Is application.properties a configuration file?
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.
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.

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.