spirng4 spring data jpa配置记录
程序员文章站
2022-06-05 15:25:26
...
本文来自http://fair-jm.iteye.com/ 转截请注明出处
前几天看spring实战(第四版)的spring-data-jpa部分 自己也动手实践了下 发现了一些问题
这边记录一下问题排除后的配置
首先是pom,这本书最不好的一点就是不告诉你要哪些依赖包。
我在动手实践的时候发现hibernate 5.2.1和spring4.2会出现flashMode方法找不到,查了下发现是hibernate在一个版本突然移除了这个方法,结果spring4.2以上跪了,我用的是spring4.3.2但是依赖的最新的spring-data-jpa依旧间接依赖4.2.6的spring-jdbc和spring-orm,在pom里直接依赖4.3.2的以上两个jar后问题排除。
pom(也包含web的 看这本书一路配过来的):
<properties> <!-- Generic properties --> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- Web --> <servlet.version>3.1.0</servlet.version> <!-- Spring --> <spring-framework.version>4.3.2.RELEASE</spring-framework.version> <spirng-data-jpa.version>1.10.2.RELEASE</spirng-data-jpa.version> <!-- Logging --> <logback.version>1.1.7</logback.version> <slf4j.version>1.7.21</slf4j.version> <!-- Test --> <junit.version>4.11</junit.version> <!-- Other --> <common-lang.version>3.4</common-lang.version> <mysql.version>5.1.39</mysql.version> <hibernate-jpa.version>1.0.1.Final</hibernate-jpa.version> <hibernate.version>5.2.1.Final</hibernate.version> <druid.version>1.0.24</druid.version> <jackson.version>2.8.1</jackson.version> <aspectj.version>1.8.9</aspectj.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-framework.version}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>${spirng-data-jpa.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring-framework.version}</version> </dependency> <!-- Other Web dependencies --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <!-- Spring and Transactions --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring-framework.version}</version> </dependency> <!-- Logging with SLF4J & LogBack --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> <!-- db related --> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>${hibernate-jpa.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- third part tools --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${common-lang.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspectj.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency> <!-- Test Artifacts --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring-framework.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies>
db的配置和书上也有少许不同:
@Configuration //repository所在的package @EnableJpaRepositories(basePackages = "com.xxx.db") @EnableTransactionManagement @Profile("dev") public class DbDevConfig { @Bean(initMethod = "init", destroyMethod = "close") public DataSource dataSource() { final DruidDataSource source = new DruidDataSource(); source.setUrl("jdbc:mysql://localhost:3306/xxx"); source.setUsername(""); source.setPassword(""); source.setInitialSize(1); source.setMinIdle(1); source.setMaxActive(20); source.setDriverClassName("com.mysql.jdbc.Driver"); return source; } @Bean public JpaVendorAdapter jpaVendorAdapter() { final HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter(); adapter.setDatabase(Database.MYSQL); adapter.setShowSql(true); adapter.setGenerateDdl(false); adapter.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect"); return adapter; } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { final LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean(); emfb.setDataSource(dataSource()); emfb.setJpaVendorAdapter(jpaVendorAdapter()); //实体所在的package emfb.setPackagesToScan("com.xxx.domain"); emfb.afterPropertiesSet(); return emfb; } @Bean public JpaTransactionManager transactionManager() { return new JpaTransactionManager(entityManagerFactory().getObject()); } }
书上没有配置emfb的packagesToScan,会导致运行时报persistence.xml找不到的错误,而根据书上说的用containerEntityManager的方式配置是不需要这个xml的。
在使用repository的时候如果用getOne会抛出no session的异常,而用findOne不会.估计又是惰性加载的问题。
估计用spring-boot会省力很多吧,不过排错什么的就更麻烦了。
上一篇: 详解js的事件代理(委托)
下一篇: 吃点糖不好嘛 甜甜的Lombok
推荐阅读
-
spring data jpa(一)
-
Spring Data JPA实践与学习
-
荐 一篇文章带你快速入门 Spring Data JPA
-
Spring Data Jpa 中级联操作CascadeType的含义
-
Spring Data Jpa+SpringMVC+Jquery.pagination.js实现分页示例
-
springboot使用spring-data-jpa操作MySQL数据库
-
Spring Data JPA 实现多表关联查询的示例代码
-
Spring Data JPA+kkpager实现分页功能实例
-
Spring Data Jpa+SpringMVC+Jquery.pagination.js实现分页示例
-
Spring Data JPA+kkpager实现分页功能实例