JPA : How to convert a native query result set to POJO class collection

后端 未结 21 1981
孤街浪徒
孤街浪徒 2020-11-22 09:23

I am using JPA in my project.

I came to a query in which I need to make join operation on five tables. So I created a native query which returns five fields.

21条回答
  •  情书的邮戳
    2020-11-22 09:27

    See example below for using a POJO as pseudo entity to retrieve result from native query without using complex SqlResultSetMapping. Just need two annotations, a bare @Enity and a dummy @Id in your POJO. @Id can be used on any field of your choice, an @Id field can have duplicate keys but not null values.

    Since @Enity does not map to any physical table, so this POJO is called a pseudo entity.

    Environment: eclipselink 2.5.0-RC1, jpa-2.1.0, mysql-connector-java-5.1.14

    You can download complete maven project here

    Native query is based on mysql sample employees db http://dev.mysql.com/doc/employee/en/employees-installation.html

    persistence.xml

    
    
        org.moonwave.jpa.model.pojo.Employee
        
            
            
            
            
        
    
    

    Employee.java

    package org.moonwave.jpa.model.pojo;
    
    @Entity
    public class Employee {
    
    @Id
    protected Long empNo;
    
    protected String firstName;
    protected String lastName;
    protected String title;
    
    public Long getEmpNo() {
        return empNo;
    }
    public void setEmpNo(Long empNo) {
        this.empNo = empNo;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }   
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("empNo: ").append(empNo);
        sb.append(", firstName: ").append(firstName);
        sb.append(", lastName: ").append(lastName);
        sb.append(", title: ").append(title);
        return sb.toString();
    }
    }
    

    EmployeeNativeQuery.java

    public class EmployeeNativeQuery {
    private EntityManager em;
    private EntityManagerFactory emf;
    
    public void setUp() throws Exception {
        emf=Persistence.createEntityManagerFactory("jpa-mysql");
        em=emf.createEntityManager();
    }
    public void tearDown()throws Exception {
        em.close();
        emf.close();
    }
    
    @SuppressWarnings("unchecked")
    public void query() {
        Query query = em.createNativeQuery("select e.emp_no as empNo, e.first_name as firstName, e.last_name as lastName," + 
                "t.title from employees e join titles t on e.emp_no = t.emp_no", Employee.class);
        query.setMaxResults(30);
        List list = (List) query.getResultList();
        int i = 0;
        for (Object emp : list) {
            System.out.println(++i + ": " + emp.toString());
        }
    }
    
    public static void main( String[] args ) {
        EmployeeNativeQuery test = new EmployeeNativeQuery();
        try {
            test.setUp();
            test.query();
            test.tearDown();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    }
    

提交回复
热议问题