• EJB
    July 31, 2013

EJB DC - Using JPQL constructor expressions

Guest Author

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

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() {
    public DeptAndEmp(String departmentName, String email,

String firstName, String 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)


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());

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

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.