Wednesday, 25 May 2016

How to process two un-related tables using JPA EntityManager

Please use the below lines of code to join un-related tables and populate data into appropriate entity objects here travelDetailEntity & travelEntity are said to be JPA Entities

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@PersistenceContext
private EntityManager em;

String query =
    "select travelDetail, travel from  travelDetailEntity as travelDetail, travelEntity as travel "+
    "where  travelDetail.location = 'location' and travelDetail.id = travel.id";
List<Object[]> results = em.createQuery(query).getResultList();

for (Object[] travelDetailAndtravel: results) {
    travelDetailEntity travelDetail = (travelDetailEntity) travelDetailAndtravel[0];
    travelEntity travel = (travelEntity) travelDetailAndtravel[1];  
}

No comments:

Post a Comment