Java Persistence Query Language

Thursday Jun 15, 2006

Named Query in Java Persistence Query Language

Named queries are static queries expressed in metadata. Named queries
can be defined in Java Persistence Query Language or in SQL.


The following is an example of the definition of an Java Persistence named query:


@NamedQuery (

    name = "findAllCustomerByName",

    query = "SELECT c FROM Customer c WHERE c.name LIKE :custName"

)


public class Customer {

...

}


Query names are scoped to the persistence unit. Its definition should
be put before the entity class definition. The above named query is
before Customer class definition.


The following is an example of the use of a named query:


@PersistenceContext public EntityManager em;


List customers = em.createNamedQuery("findAllCustomersByName")

    .setParameter("custName", "Smith")

    .getResultList();


Named queries also can be group together. Use @NamedQueries annotation to group mutiple named queries together. For example,


@NamedQueries({

@NamedQuery(

        name="findAllCustomersWithLike",

        queryString="SELECT
OBJECT(thecust) FROM Customer thecust where thecust.name like :name and
thecust.city like :city"),

@NamedQuery(

        name="findAllCustomers",

        queryString="SELECT OBJECT(thecust) FROM Customer thecust")

})







Comments:

But, if I need a expression like this: Select * from table where name LIKE '%"+name+"%' order by Id"); How transform it in NamedQuery?

Posted by Denise on October 03, 2006 at 05:44 AM PDT #

If you need an expression like this:

Select * from table where name LIKE '%"+name+"%'";

This is the named query you should write:

"SELECT p FROM Person p WHERE p.name LIKE :name"

Then, in the place where you use the query, write:

EntityManager.createNamedQuery("QueryName").setParameter("name", "%" + paramName + "%").getResultList()

Then just sort the collection by Id and voilla

Posted by Max on January 24, 2007 at 10:16 AM PST #

I add to searh with case insensitive.
Does it possible with NamedQuery?

Posted by Laurent on September 19, 2007 at 02:11 AM PDT #

What if i want to retrieve data from multiple tables.
Eg. Select i.itemname , c.category from item i, category c where i.cid=c.cid

Posted by pranay on May 09, 2008 at 01:54 AM PDT #

Hola a todos, es posible agregar un distinct pero sobre una sola columna.Mi query es el siguiente:
@NamedQuery(name = "RegistroActividades.findSemana", query = "select actividad from RegistroActividades actividad where actividad.consultores.id=:semanaConsultor"),
Lo utilizo para una lista, pero como hay datos que se repiten necesito utilizar el distinct pero sobre una solo columna.
Como le hago

Posted by Blanca on July 24, 2008 at 07:05 AM PDT #

what if i want to use the MINUS key word and Ignorecase in JPQL

Posted by shal on November 26, 2008 at 04:37 AM PST #

dfbv

Posted by 122.160.87.40 on January 03, 2009 at 02:55 AM PST #

1111111111111111

Posted by 41.224.253.241 on June 23, 2009 at 03:50 AM PDT #

<html>

Posted by 65.200.37.202 on July 15, 2009 at 03:25 PM PDT #

<b>HI</b>

Posted by 65.200.37.202 on July 15, 2009 at 03:26 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Calendar

Feeds

Search

Links

Navigation

Referrers