Java Reference
In-Depth Information
String queryString= "SELECT a FROM
Student a "+" WHERE
FUNCTION('MONTH',a.birthdate) = 7 ";
Query query = em.createQuery(queryString);
System.out.println("result :
"+query.getResultList());
TREAT : This keyword allows you to do the downcasting of an entity in order
to obtain a subclass state. It is used in the FROM and WHERE clauses. In the
following code, the entity Appuser inherits from the entity Person ; with the
keyword TREAT we can put conditions on attributes that are not contained in
the base entity ( Person ).
//Entity downcasting
String queryString = "SELECT a FROM
Person a "+" WHERE TYPE(a) = Appuser AND
"+" TREAT(a AS Appuser).userLogin =
'adwiner'";
Query query = em.createQuery(queryString);
System.out.println("result :
"+query.getResultList());
Support for creating named queries at runtime
Before JPA 2.1, named queries were defined statically as metadata before compiling
the program. Through the addNamedQuery method that was added to the
EntityManagerFactory interface, you can now create a named query at runtime
as shown in the following code:
EntityManagerFactory emf
=Persistence.createEntityManagerFactory("chapter04PU");
EntityManager em = emf.createEntityManager();
Query query = em.createQuery("SELECT a FROM
Student a");
emf.addNamedQuery("runtimeNamedQuery", query);
Search WWH ::




Custom Search