2012-04-26 5 views

ответ

2

Вы можете использовать запрос, указанную выше наряду с EntityManager's createNativeQuery функции см пример класса ниже :

package facades; 

import javax.ejb.LocalBean; 
import javax.ejb.Stateless; 
import javax.persistence.EntityManager; 
import javax.persistence.PersistenceContext; 
import javax.persistence.Query; 

@Stateless 
@LocalBean 
public class CustomFacade { 

    @PersistenceContext(unitName = "TestJPQLPU") 
    private EntityManager em; 

    public CustomFacade(){} 

    /** 
    * Gets the count of all records in tables foo and bar. 
    * @return number of records as Long. 
    */ 
    public Long getCountOfObjects(){  
     Query countQuery = em.createNativeQuery("SELECT((SELECT COUNT(*) FROM Foo) + (SELECT COUNT(*) FROM Bar))"); 
     Long count = (Long) countQuery.getSingleResult();   
     return count; 
    } 
}