Configuring JPA / Hibernate / PostgreSQL without XML

I am returning to the Java world and I am trying to set up a new Spring web application with JPA, Hibernate and PostgreSQL.

I found many old examples with various XML configuration files, and I wonder if there is a preferred new way to do this configuration without relying on creating XML files.

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

+2
source share
2 answers

Put the following snippets in a class annotated with @Configurationand@EnableTransactionManagement

Hibernate / JPA (edit the PackageToScan line):

@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 ( , ):

@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;
}

:

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(emf);
    return transactionManager;
}
+6

spring, Spring Boot, . application.properties dialect :

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

Spring Boot , . , . , , -, , 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>

, spring boot , , jar-. spring-boot-starter-data-jpa, :

  • Hibernate - JPA.
  • Spring JPA - JPA.
  • Spring ORMs - Core ORM spring Framework.

JPA spring.jpa.*. , , application.properties:

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

spring boot

+3

Source: https://habr.com/ru/post/1623891/


All Articles