2020-04-16

Hibernate JPA @NamedQueries Example

@NamedQueries

The @NamedQueries annotation is used to group multiple @NamedQuery annotations. NamedQueries specifies multiple named Java Persistence query language queries. Query names are scoped to the persistence unit. The NamedQueries annotation can be applied to an entity or mapped superclass.

Example:


@NamedQueries({
    @NamedQuery(
        name = "get_phone_by_number",
        query = "select p " +
                "from Phone p " +
                "where p.number = :number",
        timeout = 1,
        readOnly = true
    )
})

Phone phone = entityManager
.createNamedQuery( "get_phone_by_number", Phone.class )
.setParameter( "number", "123-456-7890" )
.getSingleResult();


No comments:

Post a Comment