介绍
JPA(Java Persistence API)是Sun官方提出的Java持久化规范。它为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的关系数据。他的出现主要是为了简化现有的持久化开发工作和整合ORM技术,结束现在Hibernate,TopLink,JDO等ORM框架各自为营的局面。值得注意的是,JPA是在充分吸收了现有Hibernate,TopLink,JDO等ORM框架的基础上发展而来的,具有易于使用,伸缩性强等优点。
基本查询
-预先生成方法
-自定义简单查询
Keyword
|
Sample
|
JPQL snippet
|
And
|
findByLastnameAndFirstname
|
… where x.lastname = ?1 and x.firstname = ?2
|
Or
|
findByLastnameOrFirstname
|
… where x.lastname = ?1 or x.firstname = ?2
|
Is,Equals
|
findByFirstnameIs,findByFirstnameEquals
|
… where x.firstname = ?1
|
Between
|
findByStartDateBetween
|
… where x.startDate between ?1 and ?2
|
LessThan
|
findByAgeLessThan
|
… where x.age < ?1
|
LessThanEqual
|
findByAgeLessThanEqual
|
… where x.age ⇐ ?1
|
GreaterThan
|
findByAgeGreaterThan
|
… where x.age > ?1
|
GreaterThanEqual
|
findByAgeGreaterThanEqual
|
… where x.age >= ?1
|
After
|
findByStartDateAfter
|
… where x.startDate > ?1
|
Before
|
findByStartDateBefore
|
… where x.startDate < ?1
|
IsNull
|
findByAgeIsNull
|
… where x.age is null
|
IsNotNull,NotNull
|
findByAge(Is)NotNull
|
… where x.age not null
|
Like
|
findByFirstnameLike
|
… where x.firstname like ?1
|
NotLike
|
findByFirstnameNotLike
|
… where x.firstname not like ?1
|
StartingWith
|
findByFirstnameStartingWith
|
… where x.firstname like ?1 (parameter bound with appended %)
|
EndingWith
|
findByFirstnameEndingWith
|
… where x.firstname like ?1 (parameter bound with prepended %)
|
Containing
|
findByFirstnameContaining
|
… where x.firstname like ?1 (parameter bound wrapped in %)
|
OrderBy
|
findByAgeOrderByLastnameDesc
|
… where x.age = ?1 order by x.lastname desc
|
Not
|
findByLastnameNot
|
… where x.lastname <> ?1
|
In
|
findByAgeIn(Collection ages)
|
… where x.age in ?1
|
NotIn
|
findByAgeNotIn(Collection age)
|
… where x.age not in ?1
|
TRUE
|
findByActiveTrue()
|
… where x.active = true
|
FALSE
|
findByActiveFalse()
|
… where x.active = false
|
IgnoreCase
|
findByFirstnameIgnoreCase
|
… where UPPER(x.firstame) = UPPER(?1)
|
复杂查询
-分页查询
-限制查询
-自定义SQL查询
-多表查询
- 利用hibernate的级联查询来实现(注解@OneToOne @OneToMany @ManyToOne @ManyToMany )
- 自定义一个类来接收连表查询后的结果
注意:@Query(“HQL语句 ”) @Query(“SQL语句”,nativeQuery=true)
异常: 结果集没有序列化且忘记写getter/setter方法
"Could not write JSON: No serializer found for class com.example.jpatest2.dto.CourseDetailDTO and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class com.example.jpatest2.dto.CourseDetailDTO and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0])"
补充
-spring.jpa.properties.hibernate.ddl-auto几种配置
create:每次加载Hibernate时都会删除上一次生成的表,然后重新生成新表,即使两次没有任何修改也会这样执行,这就导致每次启动都是一个新的数据库,也是导致数据丢失的重要原因。
create-drop:每次加载Hibernate时都会生成表,但当SessionFactory关闭时,所生成的表将自动删除。
update:最常用的属性值,第一次加载Hibernate时创建数据表(前提是需要先有数据库),以后加载HIbernate时只会根据model更新,即使model已经删除了某些属性,数据表也不会随之删除字段。
validate:每次加载Hibernate时都会验证数据表结构,只会和已经存在的数据表进行比较,根据model修改表结构,但不会创建新表。
-JPA通用策略生成器
- TABLE:使用一个特定的数据库表格来保存主键。
- SEQUENCE:根据底层数据库的序列来生成主键,条件是数据库支持序列。
- IDENTITY:主键由数据库自动生成(主要是自动增长型)
- AUTO:主键由程序控制。
-Lazy Loading
- Initial load time much smaller than in the other approach
- Less memory consumption than in the other approach
- Delayed initialization might impact performance during unwanted moments
- In some cases you need to handle lazily-initialized objects with a special care or you might end up with an exception
-Eager Loading:
- No delayed initialization related performance impacts
- Long initial loading time
- Loading too much unnecessary data might impact performance