2012-04-26 88 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; 
    } 
}