Repository navigation
Conversation
|
Thanks @njr-11, should all be fixed now. By the way, we need to check we're not using I didn't know about this can't-contain-null-elements thing until recently. What a landmine that is. |
| for (int i = 0; i < elements.size(); i++) { | ||
| final var element = elements.get(i); | ||
| parameters[i] = Parameter.of(element.getJavaType(), i, element.getAlias()); |
There was a problem hiding this comment.
This creates a Parameter with a position that is 0-based, although JPQL is has 1-based positional parameters and the example in the PR shows 1-based usage, so I am guess this was a mistake here. In any case, the Javadoc should probably indicate whether a 1-based or 0-based position must be used so that users don't need to make assumptions. Also, it is assigning a position value regardless of whether this is a named or positional parameter, which contradicts the getPosition Javadoc that says "null if the parameter is not a positional parameter".
I think the following would address the above,
| for (int i = 0; i < elements.size(); i++) { | |
| final var element = elements.get(i); | |
| parameters[i] = Parameter.of(element.getJavaType(), i, element.getAlias()); | |
| for (int i = 0; i < elements.size(); i++) { | |
| final var element = elements.get(i); | |
| final String alias = element.getAlias(); | |
| parameters[i] = Parameter.of(element.getJavaType(), | |
| alias == null ? (i + 1) : null, | |
| alias); |
or should the position value have been saved within the tuple element instead?
There was a problem hiding this comment.
This creates a Parameter with a position that is 0-based, although JPQL is has 1-based positional parameters
Oops, that's definitely wrong!
Also, it is assigning a position value regardless of whether this is a named or positional parameter, which contradicts the getPosition Javadoc that says "null if the parameter is not a positional parameter".
I'm not sure I think it's a huge problem if a Parameter has both a position and a name. It is in principle possible for the annotation processor to sort that out by looking at the SQL query string, but I'm also not sure I want to require that.
c57cb0c to
fb1c872
Compare
| parameters[i] = | ||
| Parameter.of(element.getJavaType(), | ||
| i+1, element.getAlias()); | ||
| } |
There was a problem hiding this comment.
Looking at this again, this is now fixed to use 1-based positional parameters rather than 0-based. However, it still seems to produce broken behavior. I tried out the following:
StaticStatementReference ref = new StaticStatementReference(
"positionalOnly", Repo.class, "positionalOnly",
Tuple.of(Parameter.of(Integer.class, 2, null).bind(4),
Parameter.of(String.class, 1, null).bind("Jakarta Persistence")));
for (int i = 0; i < 2; i++)
System.out.println("#" + ref.getParameters().get(i).getPosition() +
": " + ref.getArguments().get(i));which prints:
#1: 4
#2: Jakarta Persistence
The positional parameter indices don't match what the example code supplied. The value entered by the example code is lost and it is recomputed based on the order of the tuples.
There was a problem hiding this comment.
Yes, that's correct. That's what I was pointing out above:
An objection is that we really just create the
Parameterto immediately throw it away. It would be easily possible to introduce an Argument type packaging aParameterwith aTuple.Entry. The only issue with that is that to get it back out of theTuplewould require a type cast.
To be clear, the strong expectation is that the tuple order corresponds to the parameter order. So I would not go so far as to call it "broken".
So, y'know, there's two alternative routes:
- introduce some sort of
Argumenttype, and probably unwind the integration withTuple. - double down on
Tupleand just instantiate theTuple.Entryinstances directly.
Just not sure which direction to take this...
njr-11
left a comment
There was a problem hiding this comment.
An AI review of this PR informed me that ch05-metamodel-api.adoc has examples that no longer compile after the improvements in this PR and need to be updated:
public static TypedQueryReference<R> m(T1 p1, T2 p2, ...) {
return new StaticTypedQueryReference(
"X.m",
X.class,
"m",
R.class,
List.of(T1.class, T2.class, ... ),
List.of("p1", "p2", ... ),
List.of(p1, p2, ... ),
g,
Map.of("h1", v1, "h2", v2, ...),
o1, o2, o3, ...
);and
public static StatementReference m(T1 p1, T2 p2, ...) {
return new StaticStatementReference(
"X.m",
X.class,
"m",
List.of(T1.class, T2.class, ... ),
List.of("p1", "p2", ... ),
List.of(p1, p2, ... ),
Map.of("h1", v1, "h2", v2, ...),
o1, o2, o3, ...
);
}A few more comments from AI:
Tuple.getEntries() is still abstract on a Jakarta Persistence interface that has existed since 2.0. Existing provider implementations won't compile unless it is made a default method derived from getElements() and toArray(). Reference.getParameters() and getArgumentTuple() are also abstract, but only on new 4.0 types.
and
Primitive types: Class.cast in TupleRecord.get(..., Class) and get(TupleElement) throws a ClassCastException for int.class with a boxed value. Generators may well emit int.class for m(int x). Entry.of and bind don't validate that the value matches the type.
Also, I added some additional suggestions based on the AI review
Yes yes, I have not updated the spec yet. Sorry, I probably should have put the PR explicitly in draft mode. |
|
Thanks for this, Gavin - the design looks promising. Once there’s TCK coverage, I’ll test it against the original reproducer to verify that it resolves the null-argument problem. |
|
So I had been letting this one stew because I really wasn't sure it was the right direction. Even after all that stewing, I still don't hate this, because it opens up interesting future possibilities. In a future release of JPA we could easily require that the static metamodel include a model of parameters for named queries, allowing code like this: @NamedQuery(name = "booksByTitleAndYear",
resultClass = Book.class,
query = "from Book where title like :pattern and year >= :year")
interface Queries {}import static com.acme.Queries_.*;
var theGreatGatsby =
agent.createQuery(_booksByTitleAndYear_)
.setParameter(_booksByTitleAndYear_title, "The Great Gatsby")
.setParameter(_booksByTitleAndYear_year, 1925)
.getSingleResult();where the metamodel class is generated as: @StaticMetamodel(Queries.class)
public class Queries_ {
...
public static volatile TypedQueryReference<Book> _booksByTitleAndYear_;
public static Parameter<String> _booksByTitleAndYear_title = Parameter.of(String.class, 1, "title");
public static Parameter<String> _booksByTitleAndYear_year = Parameter.of(Integer.class, 2, "year");
}And from this perspective, the design here is completely natural, and the code for instantiating a static statement reference could just reuse the static metamodel of the In Nathan's example you would have: new StaticStatementReference(
"positionalOnly", Repo.class, "positionalOnly",
Tuple.of(_positionOnly_1.bind(4),
_positionOnly_2.bind("Jakarta Persistence")));Can you bind them out of order, yes, that's possible, but I don't think there's any other solution which fixes that, and this is generated code anyway. Nevertheless, it's a kind of a bad moment to bite this off, so let me see if I can find a way to solve the immediate problem that leaves this open as an option for the future. |
and use Tuples to create static References
Co-authored-by: Nathan Rauh <nathan.rauh@us.ibm.com>
Co-authored-by: Nathan Rauh <nathan.rauh@us.ibm.com>
Co-authored-by: Nathan Rauh <nathan.rauh@us.ibm.com>
and some missing nullability annotations
Co-authored-by: Nathan Rauh <nathan.rauh@us.ibm.com>
509ed15 to
002cc94
Compare
|
Alright, so I spent some time going around and around in circles on this one, and in the end I have a slightly-simplified version of what I had originally proposed. The difference is that this one doesn't try to reach all the way out to representing list of arguments as Please take a look @njr-11 @marko-bekhta @sebersole @lucamolteni |
002cc94 to
816b652
Compare
816b652 to
a5f3a6e
Compare
See #1193.
This enabled code like the following to create a static query reference:
Thoughts?
An objection is that we really just create the
Parameterto immediately throw it away. It would be easily possible to introduce anArgumenttype packaging aParameterwith aTuple.Entry. The only issue with that is that to get it back out of theTuplewould require a type cast.