Switch to full style
Java persistent API
Post a reply

EJB-QL IN where query

Tue Mar 23, 2010 11:17 pm

Following code use EJB QL to select a group a topics by list of dates .

Using the entity manager
Code:
List<Topic> results = entitymanager.createQuery(  
    
"select t from Topic t where t.creationDate in (:datesList)")  
    
.setParameter("datesList", datesList)  
    
.getResultList(); 


You can put this query in orm.xml as named query :

Code:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd">

    <named-query name="getTopicsFromList">
        <query>select t from Topic t where t.creationDate in (:datesList)</query>
    </named-query>
</entity-mappings>



In this case you will need to call entitymanager create name query function :

Code:
 
public List
<Topic> getTopicsFromDates(List<Date> dateList) {
    List<Topic> topicList = null; 
    javax
.persistence.Query query;
    try {
      query = entitymanager.createNamedQuery("getTopicsFromList"); 
      query
.setParameter("datesList", dateList)  ;
      topicList  = query.getResultList();
 
            
} catch (Exception exception) {
           
exception.printStackTrace();
             }
        return topicList ;
}
 




Post a reply
  Related Posts  to : EJB-QL IN where query
 SQL LIKE query Command     -  
 query about execution     -  
 insert query example     -  
 Query about jsp and printer     -  
 update query example     -  
 using parameter with name query     -  
 Run query and search from ASP     -  
 more than one parameter with name query     -  
 SQL AND OR in WHERE query Commands     -  
 select query example in php     -  

Topic Tags

Java JPA