不使用XML配置JPA / Hibernate / PostgreSQL [英] Configure JPA/Hibernate/PostgreSQL without XML

查看:600
本文介绍了不使用XML配置JPA / Hibernate / PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重新回到了Java世界,我试图用JPA,Hibernate和PostgreSQL配置一个新的Spring web应用程序。



我找到了很多具有各种XML配置文件的旧例子,我想知道是否有一种首选的新方法来执行此配置而不依赖于XML文件创作。



有些我需要配置的东西是hibernate的sql语言,驱动程序等。 解决方案

将以下片段放入注释类使用 @Configuration @EnableTransactionManagement



Hibernate / JPA (编辑packagesToScan字符串):

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


属性additionalProperties(){
属性properties = new Properties();
properties.setProperty(hibernate.hbm2ddl.auto,update);
properties.setProperty(hibernate.dialect,org.hibernate.dialect.PostgreSQL9Dialect);
properties.setProperty(hibernate.show_sql,true);
返回属性;

$ / code>

数据源(编辑用户名,密码和主机地址):

  @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();
返回dataSource;



$ b $ p


$ b

pre> @Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
返回transactionManager;
}


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.

解决方案

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

这篇关于不使用XML配置JPA / Hibernate / PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆