不使用XML配置JPA / Hibernate / PostgreSQL

编程入门 行业动态 更新时间:2024-10-23 07:15:28
本文介绍了不使用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

本文发布于:2023-10-27 23:26:29,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JPA   XML   PostgreSQL   Hibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!