如何在java createQuery中创建此查询字符串?

时间:2014-09-07 20:37:30

标签: java sql hibernate jpa native

我正在尝试使用Hibernate或本机SQL或java来返回Product个对象的列表,但我无法使代码正常工作,如果有人帮助我,我很感激,非常感谢,     这是逻辑:

 ...
 ArrayList<Product> prodList = new ArrayList<Product>();
EntityManager entityManager = this.entityManager;
try 
{

  //the logic of following query is: select all records from prodTable table where productDate is
//passed 2 months than today's date(something list today's day minus productDate is 2 months), 
//and then the records are not existed in prodCategory table by the key of ProductID and 
//productCategory combination. So the prodList is the combination of the above two conditions. 
//something like this:

 String queryString = "select p from prodTable p where (today - p.ProductDate) greaterThan 2  
months AND p is not in (select c from prodCategory c where c.ProductID = p.ProductID and 
c.category = '09' ) //need help of this line of query.

 prodList = entityManager.createQuery(queryString).getResultList();
 return prodList;
} catch (Exception e) {

throw e;

} ...

1 个答案:

答案 0 :(得分:0)

尝试以下

 ...
 ArrayList<Product> prodList = new ArrayList<Product>();
EntityManager entityManager = this.entityManager;
try 
{

 String queryString = "select p from prodTable p where (today - p.ProductDate) greaterThan 2  
months AND p is not in (select c from prodCategory c where c.ProductID = p.ProductID and 
c.category = '09' )"; //need help of this line of query.

 prodList = entityManager.createSQLQuery(queryString).getResultList();
 return prodList;
} catch (Exception e) {

throw e;
}

Note:我使用过SQLQuery,因此您可以在编写查询时放置查询。希望它有所帮助。