-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
null pointer in panache 'list' query with like clause #33310
Comments
/cc @FroMage (panache), @loicmathieu (panache) |
hey, it seems that in the GdwLksCptRd.find( "GdwLksCptRd.search", Parameters.with( "criterion", criterion + "%" ) ).range( 0, 29 ).list() The named query is missing a
so something like this: GdwLksCptRd.find( "#GdwLksCptRd.search", Parameters.with( "criterion", criterion + "%" ) ).range( 0, 29 ).list() should work (at least I've tested it without the |
Yes, named query in a PanacheQL query (the simplified JPQL that is accepted by all Panache methods) must start with @sjaakd can you confirm that it's working ? |
Hmmm, if it fixes it, we definitely need to improve and provide a proper error message and not a NPE. |
Thanks guys. I'll check that one asap.. Need to rewrite quite some code to test again, so will need some time. Could it be so simple? I tried querying directly: Yes.. the NPE put me on a wrong track 😄 .. |
Definitely. I'll track the ORM side - however this issue only applies to ORM5, as ORM6 treats functions in an entirely different way. On the Panache side, it would be nice to detect that the simplified query is invalid and it happens to refer to a named query - not sure if we can do better. |
@Sanne thanks.. In my case I have a number of quite similar objects that share all the same So I've got a number of maps handling a function that calls this named query. This is how it looked like: private static final List<BiFunction<EntityManager, String, List<ObjectDto>>> DINO_OBJECT_QUERIES = List.of(
(e, criterion) -> objectQuery( e, "GdwLksGboRd.search", criterion ),
(e, criterion) -> objectQuery( e, "GdwLksAboRd.search", criterion ),
(e, criterion) -> objectQuery( e, "GdwLksGsoRd.search", criterion )
// etc
); and then: private static List<ObjectDto> objectQuery(EntityManager em, String queryName, String criterion ) {
TypedQuery<ObjectDto> query = em.createNamedQuery( queryName, ObjectDto.class );
query.setParameter( "criterion", criterion + "%" );
query.setMaxResults( MAX_SEARCH_RESULTS_PER_DATA_TYPE );
return query.getResultList();
}
Now.. I want to rewrite this using panache and panache projection So: private static final List<Function< String, List<ObjectDto>>> DINO_OBJECT_QUERIES = List.of(
criterion -> objectQuery( GdwLksGboRd.find("#GdwLksGboRd.search", Parameters.with( "criterion", criterion + "%" ) ) ),
criterion -> objectQuery( GdwLksAboRd.find("#GdwLksAboRd.search", Parameters.with( "criterion", criterion + "%" ) ) ),
criterion -> objectQuery( GdwLksGsoRd.find("#GdwLksGsoRd.search", Parameters.with( "criterion", criterion + "%" ) ) ),
//: private static <T extends PanacheEntityBase> List<ObjectDto> objectQuery( PanacheQuery<T> query ) {
return query.range( 0, MAX_SEARCH_RESULTS_PER_DATA_TYPE ).project( ObjectDto.class ).list();
} example query: @NamedQuery(
name = "GdwLksBhrGRd.search",
query = "select p.broId, p.geometry, 'SOIL', 'BRO', 'bhrg' "
+ "from GdwLksBhrGRd p where p.broId like :criterion order by p.broId"
) Only to find out that Panache does not support |
@FroMage ^ |
Ok.. I'm going to stop rewriting the named queries to Panache. I've found some more issues. I'll try to summarize them. I've removed the named queries in favor of simple queries like this:
public ObjectDto(@ProjectedFieldName( "broId" ) String objectId, @ProjectedFieldName( "geometry" ) Geometry geometry) {
this.objectId = objectId;
if ( geometry != null ) {
this.xCoordinate = geometry.getCoordinate().x;
this.yCoordinate = geometry.getCoordinate().y;
}
}
// adding this constructor gives the NPE
public ObjectDto(@ProjectedFieldName( "dinoNumber" ) String objectId, @ProjectedFieldName( "xRdCrd" ) BigDecimal xCoordinate, @ProjectedFieldName( "yRdCrd" ) BigDecimal yCoordinate) {
this.objectId = objectId;
if ( xCoordinate != null ) {
this.xCoordinate = xCoordinate.doubleValue();
}
if ( yCoordinate != null ) {
this.yCoordinate = yCoordinate.doubleValue();
}
}
The next point is that Panache will not allow me to use string literals in the "new Object" in projection, like this: @NamedQuery(
name = "GdwLksCptRd.search",
query = "select new nl.bro.microservices.dlp.model.common.ObjectDto( p.broId, p.geometry, 'SOIL', 'BRO', 'cpt' ) "
+ "from GdwLksCptRd p where p.broId like :criterion order by p.broId"
) this means that I've got to iterate over the result list and set the proper parameters afterwards. I cannot use the flexibility that the Last, but not least for you guys: I can confirm that the issue was indeed the missing I hope you can somehow use my feedback above to improve on the Panache library. I really would like to use it. Especially when combining it with its accompanying unit test library. Now I've somehow got the figure out how to mock an Oracle DB in unit test (with native types in H2) or mock the entity manager. |
Great feedback, thanks! Regarding the Oracle DB specifically.. why not use the dev-services facility? I believe there's no better way to test than to test on the real thing.. it will start a real Oracle container instance for you during tests; if you configure testcontainers for container reuse, it will also be very fast as it won't have to start the DB container often. |
Happy to be useful 😄 . Wrt to Oracle: we are possibly moving away from Oracle in the future. License costs / opensource vs closed source etc. In short, it comes down what you consider to be abstraction. I think an ORM should provide database abstraction, not needing a dao, but my experience is otherwise. I try to encourage to use as much as standard SQL as possible in the teams. I just recently discovered rider-cdi and I really like it in combination with h2 and Quarkus. With a few annotations you setup your test data. Keep up the good work here.. Its appreciated 👍 . BTW: If you want to close the issue that's ok for me. I leave that up to you. My issues are handled. |
So, this issue turns out to have many parts:
Thanks for your feedback, this is very helpful. |
FWIW https://hibernate.atlassian.net/browse/HHH-16609 has been fixed upstream, so the originally reported problems will be fixed in Quarkus as soon as Hibernate ORM 5.6.16 gets released and we upgrade in Quarkus 2.16. @FroMage can you confirm I can close this ticket as soon as the upstream issue in ORM gets fixed in Quarkus, or do you need to keep it open to track those other problems you listed? I'd recommend opening another issue, but it's up to you. |
Most of these points I summarised are taken care of by #33902 if you want to review it. |
Hi guys, Although closed I see I missed some questions. Sorry. For that "The next point is that Panache will not allow me to use string literals in the "new Object" in projection, like this:" what's your error? I don't see why Panache would be involved in that one. -> Panache is not involved here. It's just a feature that you get with standard named queries.. Just like this
Note that you can set the constructor arguments with a literal? I miss that kind of functionality in Panache. "I cannot use the flexibility that the @NamedQuery offers assigning different query parameters". Can you clarify? I don't understand the issue. -> I'm lost here myself as well 😢 |
Oh, OK, so you'd like to use literal values for some of the parameters of your projection constructor, that don't correspond to selected columns? Perhaps some extra optional annotation parameter on That could be useful, could you open an issue for that please? This way we can see if more people would find this useful. Thanks. |
Just an idea, but at that point you could just allow calling a static factory method instead of a constructor, and let users define that method and handle the default values. It could be even more powerful than annotations. |
Yes, true. Though this would not use HQL projection, right? |
I don't think HQL supports calling factory methods at the moment, no. |
Describe the bug
Leads to:
Same result for executing the query with
@NamedQuery
as arg:However executing a normal JPA query works as expected.
Expected behavior
No response
Actual behavior
No response
How to Reproduce?
No response
Output of
uname -a
orver
No response
Output of
java -version
openjdk 11
GraalVM version (if different from Java)
No response
Quarkus version or git rev
2.16.6
Build tool (ie. output of
mvnw --version
orgradlew --version
)mvn
Additional information
No response
The text was updated successfully, but these errors were encountered: