skip to Main Content

In spring, we can declare a custom query like this

@Query(
  value = "SELECT * FROM USERS u WHERE u.status = 1", 
  nativeQuery = true)
Collection<User> findAllActiveUsersNative();

but in Quarkus I cannot find a reference on how to do this in quarkus

Does anyone have any experience with how to do this?

2

Answers


  1. Write something like this in your User entity (which should extend PanacheEntity) :

    public static List<User> findByStatus() {
       return list("status=1");
    }
    

    See also documentation:
    https://quarkus.io/guides/hibernate-orm-panache#adding-entity-methods

    Login or Signup to reply.
  2. You need to declare your custom queries as NamedQuery.
    See: https://quarkus.io/guides/hibernate-orm-panache#named-queries

    In your scenario, it could look like this:

    @Entity(name = "USERS")
    @NamedQueries({
            @NamedQuery(name = "custom.allActiveUsers", query = "FROM USERS u WHERE u.status = 1")
    })
    public class User extends PanacheEntity implements Serializable {
    
        @Column(name = "version")
        public Long version;
        
        public static List<User> getActiveUsers() {
            return find("#custom.allActiveUsers");
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search