Package io.quarkus.hibernate.panache


package io.quarkus.hibernate.panache

API usage

Hibernate with Panache comes in two flavors, the active record pattern via
invalid reference
io.quarkus.hibernate.orm.panache.PanacheEntity
and the repository pattern via
invalid reference
io.quarkus.hibernate.orm.panache.PanacheRepository
. To use the active record pattern, make your entities extend
invalid reference
io.quarkus.hibernate.orm.panache.PanacheEntity
, use public fields for your columns, use the existing operations defined as static methods on your entity class, and define custom ones as static methods on your entity class:
 @Entity
 public class Person extends PanacheEntity {
     public String name;
     public LocalDate birth;
     public PersonStatus status;

     public static Person findByName(String name){
       return find("name", name).firstResult();
     }

     public static List<Person> findAlive(){
       return list("status", Status.Alive);
     }

     public static void deleteStefs(){
       delete("name", "Stef");
     }
 }
 
To use the repository pattern, create a class implementing
invalid reference
io.quarkus.hibernate.orm.panache.PanacheRepository
, use the existing operations from your repository and define new ones on your repository class:
 @ApplicationScoped
 public class PersonRepository implements PanacheRepository<Person> {
    public Person findByName(String name){
        return find("name", name).firstResult();
    }

    public List<Person> findAlive(){
        return list("status", Status.Alive);
    }

    public void deleteStefs(){
        delete("name", "Stef");
   }
 }
 

Simplified queries

Normally, HQL queries are of this form: from EntityName [where ...] [order by ...], with optional elements at the end.

If your select query does not start with from, select, or with, we support the following additional forms:

  • order by ... which will expand to from EntityName order by ...
  • <singleAttribute> (and single parameter) which will expand to from EntityName where <singleAttribute> = ?
  • where <query> will expand to from EntityName where <query>
  • <query> will expand to from EntityName where <query>

If your update query does not start with update from, we support the following additional forms:

  • from EntityName ... which will expand to update from EntityName ...
  • set? <singleAttribute> (and single parameter) which will expand to update from EntityName set <singleAttribute> = ?
  • set? <update-query> will expand to update from EntityName set <update-query> = ?

If your delete query does not start with delete from, we support the following additional forms:

  • from EntityName ... which will expand to delete from EntityName ...
  • <singleAttribute> (and single parameter) which will expand to delete from EntityName where <singleAttribute> = ?
  • <query> will expand to delete from EntityName where <query>
We also support named queries, for Panache to know that a query is a named query and not an HQL one, you need to prefix the name of the query with '#'.
Author:
Stéphane Épardaud