Description   Build & Configure   Run   Troubleshoot   Related Topics  Start to Run

About the Example

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.


Files Used in the Example

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.

Description

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(List tags) {
    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 List tags=new ArrayList<>();

  private LocalDate birthday;

  @Column(name = "entry_date")
  private LocalDate entrydate;
  ...
}

The EmployeeService provides to retrieve the employees as stream:

public List  findAll(){
  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;
}

Building and Configuring the Example

Prerequisites

Before working with this example:

  1. Install WebLogic Server, including the examples.
  2. Start the Examples server.
  3. Set up your environment.

Configure Oracle WebLogic Server

  No special configuration is required for this example.

Configure Example Resources (Optional)

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:

  1. Execute the following command from the shell where you set your environment:

  2. ant deploy.datasource

You can use the command ant undeploy.datasource to undeploy resource.

Build and Deploy the Example

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.

  1. Change to the D:\Oracle\Middleware\Oracle_Home\wlserver/samples/server/examples/src/examples/javaee8/jpa directory.
  2. Execute the following command:

    ant build

    to compile and stage the example.
  3. Execute the following command:

    ant deploy

    to deploy the example in the wl_server WebLogic Server Examples domain.


Running the Examples

  1. Complete the steps in the "Building and Configuring the Example" section.
  2. Execute the following command:

    ant run

    which opens your default browser to display this example application home page.
  3. If the example runs successfully, you can add employee from the server, and you also can retrieve the employees list.

Troubleshooting


Related Topics


Copyright @ 2020 Oracle and/or its affiliates. All rights reserved.