EJB DC - Using JPQL constructor expressions

In JPA 2.0, a new feature is provided - "Constructor Expressions in the SELECT Clause". This feature is mainly useful for queries with multiple Select expressions, where custom result objects is necessary. This feature works with Oracle JDeveloper 12.1.2.0.0

Implementation Steps

Create Fusion Web Application with entities based on Departments and Employees, then create a session bean.

Create a Java class "DeptAndEmp" and add the below code.

public class DeptAndEmp implements Serializable {
    private String departmentName;
    private String email;
    private String firstName;
    private String lastName;
    public DeptAndEmp() {
        super();
    }

    public DeptAndEmp(String departmentName, String email, 
		String firstName, String lastName) {
        this.setDepartmentName(departmentName);
        this.setEmail(email);
        this.setFirstName(firstName);
        this.setLastName(lastName);
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setLastName(String lastName) {
	 this.lastName = lastName;
    }
    public String getLastName() {
        return lastName;
    }
}

Open the session bean and paste the below method code and expose the method filterDeptEmpResult() method in local/remote interface.

public List<DeptAndEmp> filterDeptEmpResult() { String qlString = "SELECT NEW model.util.DeptAndEmp(dept.departmentName, emp.email, emp.firstName, emp.lastName) FROM Employees emp, Departments dept where emp.departments.departmentId = dept.departmentId"; TypedQuery<DeptAndEmp> query = em.createQuery(qlString, DeptAndEmp.class); return query.getResultList(); }

Note:- In the Query "model is a package name and TypedQuery is a JPA query that returns a specific type of Object. TypedQuery eliminates the need to cast the query result to a specific type or having to add a SuppressWarnings annotation to eliminate compiler warnings about unchecked conversions. TypedQuery is well explained in Java Persistence Architecture 2.0 - Using The New TypedQuery Interface

Create a Sample Java Client and add the below code in main method.

SessionEJB sessionEJB = (SessionEJB) context.lookup("JPAConstructorApp-Model-SessionEJB#model.SessionEJB");

for (DeptAndEmp deptAndEmp : (List<DeptAndEmp>) sessionEJB.filterDeptEmpResult()) { System.out.println("departmentName = " + deptAndEmp.getDepartmentName()); System.out.println("email = " + deptAndEmp.getEmail()); System.out.println("firstName = " + deptAndEmp.getFirstName()); System.out.println("lastName = " + deptAndEmp.getLastName()); System.out.println("======================================="); }

Deploy the sessionFacade and run the java client. In the console employees details will be displayed as below.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Tips and Tricks from Oracle's JDeveloper & ADF QA

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today