Description Build & Configure Run Troubleshoot Related Topics Start to Run
JPA 2.2 adds injection support in @AttributeConverter
. That means attribute converter classes
in Java EE environments support dependency injection through the Contexts and Dependency Injection API (CDI)
when CDI is enabled. The support for CDI injection will allow to inject your reusable conversion
implementation into an AttributeConverter
.
JPA 2.2 adds the support for mapping of the java.time.LocalDate
, java.time. LocalTime
,
java.time.LocalDateTime
, java.time.OffsetTime
, and java.time.OffsetDateTime
types. So you will no longer need the converter with JPA 2.2.
JPA 2.2 enhances the methods to retrieve the results of Query
and TypedQuery
as streams.
This example demonstrates the all new features described above.
Directory Location:
D:\Oracle\Middleware\Oracle_Home\wlserver/samples/server/examples/src/examples/javaee8/jpa/
File Click source files to view code. |
Description |
---|---|
build.xml | Ant build file that contains targets for building and running the example. |
Employee.java | Persistent entity Employee. |
ConverterUtils.java | A standalone CDI bean that includes the processing of converting a tag list to string in database. |
DataInitializer.java | Context listener used to ensure that the data for this example is ready. |
EmployeeController.java | JSF Managed Bean class to add a new employee and populate a list of existing employees. |
EmployeeService.java | EJB Stateless Session Bean that queries all employees information, and uses the Unsynchronized Persistence Context to create a new employee. |
ListToStringConveter.java | The AttributeConverter class that injects ConverterUtils implementation. |
index.xhtml | Index page for this example, which describes this feature and example and verifies the functions. |
response.xhtml | Extensible HyperText Markup Language Page containing JavaServer Faces components. |
persistence.xml | Configuration file for the entity using schema generation properties. |
web.xml | Web application deployment descriptor. |
beans.xml | CDI deployment descriptor file to enable CDI features. |
This example demonstrates the new features of JPA 2.2, such as injection support in @AttributeConverter,
the support for new Java 8 Date and Time API, and the support for retrieving the results of Query
and TypedQuery
as streams.
The ConverterUtils
includes the processing of converting a tag list to string in database:
public class ConverterUtils { public String listToString(Listtags) { return StringUtils.join(tags, ","); } public List stringToList(String str) { return Arrays.asList(str.split(",")); } }
The ListToStringConveter
injects ConverterUtils
implementation:
@Converter(autoApply=false) public class ListToStringConveter implements AttributeConverter, String> { @Inject ConverterUtils utils; @Override public String convertToDatabaseColumn(List
attribute) { if(attribute == null || attribute.isEmpty()){ return ""; } return utils.listToString(attribute); } @Override public List convertToEntityAttribute(String dbData) { if(dbData == null || dbData.trim().length() == 0){ return new ArrayList (); } String[] data = dbData.split(","); return utils.stringToList(dbData); } }
Apply the AttributeConverter class in the entity class Employee
:
@Entity @Table(name= "employee") public class Employee implements Serializable{ private static final long serialVersionUID = 1L; ... @Column(name="TAGS") @Convert(converter = ListToStringConveter.class) private Listtags=new ArrayList<>(); private LocalDate birthday; @Column(name = "entry_date") private LocalDate entrydate; ... }
The EmployeeService
provides to retrieve the employees as stream:
public ListfindAll(){ CriteriaQuery criteriaQuery = entityManager.getCriteriaBuilder().createQuery(); criteriaQuery.select(criteriaQuery.from(Employee.class)); Stream personStream = entityManager.createQuery(criteriaQuery).getResultStream(); List employees = personStream.collect(Collectors.toList()); return employees; }
Before working with this example:
No special configuration is required for this example.
The data source for this example is default preconfigured for Examples Server. If you want to run this example on a different WebLogic Server Domain, you need configure data source firstly using the follow command:
ant deploy.datasource
You can use the command ant undeploy.datasource
to undeploy resource.
This sample is already built and deployed in the wl_server
WebLogic Server Examples domain, so you can
skip these steps and proceed directly to running the example.
D:\Oracle\Middleware\Oracle_Home\wlserver/samples/server/examples/src/examples/javaee8/jpa
directory.
ant build
ant deploy
wl_server
WebLogic Server Examples
domain.ant run
Copyright @ 2020 Oracle and/or its affiliates. All rights reserved.