I'm currently building an application Quarkus and when I try to use the @NativeNamedQuery my application throws an PanacheQueryException
saying:
io.quarkus.panache.common.exception.PanacheQueryException: The named query 'ClassName.findUsingNamedNativeQuery' must be defined on your JPA entity or one of its super classes.
This is the code that I have:
@ApplicationScoped
public class MyClassRepository implements PanacheRepositoryBase<MyClass, UUID> {
public Optional<MyClass> findUsingNamedNativeQuery(UUID classId){
return find("#MyClass.findUsingNamedNativeQuery", Parameters.with("classId",classId)).singleResultOptional();
}
}
And this is my @NamedNativeQuery in My Class:
@Entity
@Table(name = "my_class")
@NamedNativeQueries({
@NamedNativeQuery(name = "MyClass.findUsingNamedNativeQuery",
query = """
SELECT mc.* FROM my_class mc
WHERE mc.id = :classId
""",
resultClass = MyClass.class)
})
public class MyClass{
...
}
This is a very strange error that I've never seen while programing in Spring or JakartaEE. Because it is complaining that the named query is not defined in JPA Entity but it is... I used the same procedures I normally used while working with JakartaEE.
Does anyone know how to use @NamedNativeQuery in Quarkus, if it is really possible?
Currently (Oct, 2023) it's not supported. Here is the issue. You can call Hibernate API directly like
final var queryString = """
SELECT mc.* FROM my_class mc
WHERE mc.id = :classId
""";
return Panache.getSession()
.map(session -> session.createNativeQuery(queryString, MyClass.class))
.map(query -> query.setParameter("classId", id))
.map(query -> query.setMaxResults(1))
.map(query -> query.getResultStream().findFirst());
}