跳至主要内容

博文

目前显示的是标签为“JPA”的博文

JPA 2.2: Support Stream as query result

Support Stream as query result Hibernate has already added a lot of alignments with Java 8 , Stream and Optional are supported. JPA 2.2 add official support for Stream result of query. The following codes demonstrates query posts by keywork in JPA 2.1. public List< Post > findByKeyword( String keyword) { CriteriaBuilder cb = this . entityManager . getCriteriaBuilder(); CriteriaQuery< Post > q = cb . createQuery( Post . class); Root< Post > c = q . from( Post . class); List< Predicate > predicates = new ArrayList<> (); if ( null != keyword && " " . equals(keyword . trim())) { predicates . add( cb . or( cb . like(c . get( Post_ . title), ' % ' + keyword + ' % ' ), cb . like(c . get( Post_ . content), ' % ' + keyword + ' % ' ) ) ); } q . where(predicates . toArray( new Predicate [predicates . size()])); TypedQuery query = this . entityMan...

JPA 2.2: Java 8 Datetime support

Java 8 Datetime support In the former version, there are some approaches to add Java Datetime support by yourself. Custom AttributeConverter , I descirbed here . Spring Data JPA also provides AttributeConverter for Java 8 DataTime supports. Unwrap EntityManager to provider's implementation, such as Hibernate's Session , which has built-in Java 8 Datetime support . JPA 2.2 adds following Java 8 Datetime APIs support by default. java.time.LocalDate java.time.LocalTime java.time.LocalDateTime java.time.OffsetTime java.time.OffsetDateTime Create entity class. @Entity public class Post extends AbstractEntity { public static enum Status { DRAFT , PUBLISHED ; } private String title; private String content; @Enumerated ( EnumType . STRING ) private Status status = Status . DRAFT ; private LocalDateTime publishedAt; private LocalDateTime createdAt; private LocalDateTime updatedAt; publi...

JPA 2.2: More CDI Alignments

More CDI Alignments In JPA 2.1, EntityListener supports @Inject (some persistence providers are still problematic). JPA 2.2 adds injection support in @AttributeConverter . Let's reuse the sample codes in my Java EE 7 sample. The following is the original version for JPA 2.1, which is used to convert a tag list to string in database. @Converter public class ListToStringConveter implements AttributeConverter< List< String > , String > { @Override public String convertToDatabaseColumn ( List< String > attribute ) { if (attribute == null || attribute . isEmpty()) { return " " ; } return StringUtils . join(attribute, " , " ); } @Override public List< String > convertToEntityAttribute ( String dbData ) { if (dbData == null || dbData . trim() . length() == 0 ) { return new ArrayList< String > (); } String []...

Building REST APIs with Spring MVC

As stated in before posts, we are going to build a Blog system. To demonstrate REST API, we use a simple Post entity to persist blog entries, and expose the CRUD operations via REST APIs to client applications. As a REST API consumer, the client applications could be a website, a desktop application, or a mobile application. Following the REST API convention and HTTP protocol specification, the post APIs can be designed as the following table. Uri Http Method Request Response Description /posts GET 200, [{'id':1, 'title'},{}] Get all posts /posts POST {'title':'test title','content':'test content'} 201 Create a new post /posts/{id} GET 200, {'id':1, 'title'} Get a post by id /posts/{id} PUT {'title':'test title','content':'test content'} 204 Update a post /posts/{id} DELETE 204 Delete a post Next, we begin to create the domain model...

JPA 2.1: Overview

JPA 2.1: Overview JPA 2.1 brings a series of small improvements. I have written some posts to introduce them one by one. Schema generation scripts configuration Attribute Converter Bulk Criterial Update and Delete API Entity Graph Synchronized Persistence Contexts Treat CDI support Programmatic Named Queries JPA 2.1 also includes some RDBMS improvements, such as supports   Store Procedure , and adds   function   in JPQL. I am not a big fan of RDBMS, so these are not so attractive for me. In this version, some features are still not perfect. As I motioned, the schema generation scripts strategy does not include a Hibernate equivalent   UPDATE . Secondly, the CDI support is omitted in the   @Converter   and   @Entity   classes. The JPA expert group should consider more about NoSQL support. Hear the voice from Hibernate community(there is a Hibernate OGM project for NoSQL, now supports Mongo, JBoss Infinispan) and other JPA pro...