1. JPA
The Java persistence API, a set of API,JPA for object persistence, is itself a set of specifications that allows developers to access different ORM frameworks in the same way. In fact, it is the Java entity object and the relational database to establish the mapping relationship, through the object-oriented programming of the concept of operating relational database specification. Therefore, the various ORM frameworks provide implementations that meet the requirements of the JPA. The most common use of ORM frameworks in spring projects is that Hibernate,hibernate Entitymanager is a provider of JPA.
2. Spring Data
Although we can access different ORM frameworks through the JPA specification, the code that accesses the ORM requires a tedious and repetitive process of resource acquisition, statement creation, exception handling, and resource release. The support provided by the Spring framework to JPA is mainly reflected in the following areas:
First, it makes the JPA configuration more flexible. The JPA specification requires that the configuration file be named Persistence.xml and exist in the Meta-inf directory under the classpath. This file typically contains all the information needed to initialize the JPA engine. The Localcontainerentitymanagerfactorybean provided by Spring provides a very flexible configuration, and the information in the Persistence.xml can be provided here in the form of attribute injection.
- Second, Spring implements some of the features that are part of the EJB container environment, such as container injection support for @PersistenceContext, @PersistenceUnit.
Third, and the most significant, Spring will entitymanager creation and destruction, transaction management and other code extraction, and by its unified management, developers do not need to care about these, business methods only left the operation of the domain object code, transaction management and Entitymanager creation, The destruction of the code no longer requires the developer's concern.
2. Spring Data JPA
The spring data JPA framework, which focuses on the only business logic code that is not simplified to the spring, has been saved by the developer's only remaining implementation of the persistence layer's business logic, and the only thing to do is to declare the interface for the persistence layer, and the rest to spring Data JPA To help you finish.
The guide to the use of the project is here http://docs.spring.io/spring-data/jpa/docs/1.9.0.RELEASE/reference/html/
3.1 Spring Data JPA core data-tier interface
The following figure is from http://blog.csdn.net/chszs/article/details/42737553, which describes the relationship of the core interface, where spring data commons is the underlying component of SPIRNG data. Spring Data JPA requires the services provided by this component.
1:repository: The topmost interface, which is an empty interface, is designed to unify all types of Repository and to automatically recognize components as they are scanned.
2:crudrepository: is a repository sub-interface that provides CRUD functionality
3:pagingandsortingrepository: is a crudrepository sub-interface that adds pagination and sorting functionality
4:jparepository: Is the Pagingandsortingrepository sub-interface, added some useful functions, such as: batch operation.
5:jpaspecificationexecutor: Used to do the interface responsible for querying
6:specification: is a query specification provided by Spring Data JPA, to make complex queries, simply set the query criteria around this specification to
The methods provided by these interfaces, which are described in the spring Data JPA official documentation, are very comprehensive and address here http://docs.spring.io/spring-data/jpa/docs/1.9.0.RELEASE/api/
3.2 Jparepository's query
Directly in the interface to define the query method, if it is compliant, can not write implementation, the currently supported keywords are as follows:
- and---are equivalent to the AND keyword in SQL, such as Findbyusernameandpassword (String user, Striang pwd);
- The or---is equivalent to the OR keyword in SQL, such as findbyusernameoraddress (string user, String addr);
- The between---is equivalent to the between keyword in SQL, such as Findbysalarybetween (int max, int min);
- The LessThan---is equivalent to "<" in SQL, such as Findbysalarylessthan (int max);
- GreaterThan---is equivalent to ">" in SQL, such as Findbysalarygreaterthan (int min);
- IsNull---is equivalent to "is null" in SQL, such as Findbyusernameisnull ();
- Isnotnull---is equivalent to "is not NULL" in SQL, such as Findbyusernameisnotnull ();
- Notnull---and isnotnull equivalence;
- The like---is equivalent to the "as" in SQL, such as Findbyusernamelike (String user);
- The notlike---is equivalent to "not like" in SQL, such as Findbyusernamenotlike (String user);
- The order-by---is equivalent to the "FINDBYUSERNAMEORDERBYSALARYASC" in SQL, such as the String user;
- The not---is equivalent to "in SQL"! = ", such as Findbyusernamenot (String user);
- In---is equivalent to ' in ' in SQL, such as Findbyusernamein (Collection<string> userlist), the parameter of the method can be either a Collection type or an array or an indefinite length parameter;
- The notin---is equivalent to "not in" SQL, such as Findbyusernamenotin (Collection<string> userlist), which can be either a Collection type or an array or not Fixed length parameters
3.3 JPA's Namedqueries
3.4 Using @query
Resources
Http://sishu ok.com/forum/posts/list/7000.html
Http://www.cnblogs.com/WangJinYang/p/4257383.html
1. For LOB types that store big data, JPA can only be annotated in Entiry @lob
LOBs represent large object data, including BLOBs and CLOB two types, which are used to store large chunks of binary data, slice data, video data, etc., while the latter is used to store long text data, such as the forum's post content, product details, etc.
It is noteworthy that: in different databases, large objects corresponding to the field type is not the same, such as DB2 corresponding blob/clob,mysql corresponding blob/longtext,sqlserver corresponding image/text.
It should be noted that some databases have large object types that can be accessed like simple types, such as MYSQL's longtext operation and the VARCHAR type. In general, LOB type data is accessed differently than other simple types of data, and we often manipulate lob-type data in a streaming manner. In addition, access to LOB type data is not thread-safe, it is necessary to allocate the appropriate database resources separately, and to release resources after the operation is complete.
(Reference: http://www.ibm.com/developerworks/cn/java/j-lo-spring-lob/)
2, I save/update cob type of data, C3P0 connection pool error:
Java.lang.AbstractMethodError:com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.setCharacterStream (iljava/io/ Reader; J) V
Find information on the Internet, found that is 0.9.1.2 Version of the C3P0 does not have Setcharacterstream method, so download C3p0-0.9.5.1.jar, put to the local repository, the next time Update/save will not error.
JPA && Spring Data JPA