根据 Spring Data JAP Guide 入门 Sping JPA 过程记录
-
环境准备
项目要去 jdk1.8 + ,Application.java 中使用了 jdk1.8 + 的lambda表达式.则需要升级JDK 与 Exclipse IDE- 升级JDK
- Eclipse IDE kepler SR2 已经支持 jdk 1.8 ,需要安装插件:
- Java 8 support for Eclipse Kepler SR2
- Java 8 support for m2e for Eclipse Kepler SR2 (optional)
- Java 8 Facet for Web Tools for Eclipse Kepler SR2 (optional)
-
按照 Guide中的步骤 实现
- 创建Maven Project
- 将依赖粘贴至 pom.xml 中
- 创建 Customer实体类等步骤
- 进入Application.java 右键 Run as –> Java Application
会有如下的异常信息:
检查 classpath中有包 javassist-3.20.0-GA.jar ,问题根源暂不确定,修改依赖版本为 3.18.1-GA 即可正常使用了.
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
</dependency>
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javassist/bytecode/ClassFile
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at hello.Application.main(Application.java:17) [classes/:na]
运行结论
运行成功后,日志能够显示出运行结果,调用 Repository中的 findOne,findAll ,save等方法都能正确运行.-
遗留问题:
- 数据保存到那里了?
- 变更 lambda 表达式中,save方法的参数,再重复执行程序, 生成的ID仍然是 1 到 5 而没有自增为 6-10, 为什么?
以上两个问题实际是因为本次运行使用了 H2内存数据库,pom.xml中有如下配置:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
内存数据库将数据保存在内存中,程序关闭后,占用的内存空间释放,所以不会出现两次程序运行 ID自增的情况。