Skip to content

Alternative solution to #1193 - #1199

Open
gavinking wants to merge 18 commits into
jakartaee:mainfrom
gavinking:tuplestuff
Open

gavinking wants to merge 18 commits into
jakartaee:mainfrom
gavinking:tuplestuff

Conversation

@gavinking

Copy link
Copy Markdown
Member

See #1193.

This enabled code like the following to create a static query reference:

        new StaticStatementReference("updateNameAndLocale", 
                Repo.class, "updateNameAndLocale",
                Tuple.of(Parameter.of(String.class, 1, "name").bind("Gavin"),
                         Parameter.of(Locale.class, 2, "locale").bind(Locale.US)));

Thoughts?

An objection is that we really just create the Parameter to immediately throw it away. It would be easily possible to introduce an Argument type packaging a Parameter with a Tuple.Entry. The only issue with that is that to get it back out of the Tuple would require a type cast.

@gavinking

Copy link
Copy Markdown
Member Author

cc @lucamolteni @sebersole

@gavinking gavinking linked an issue Oct 19, 2026 that may be closed by this pull request

@njr-11 njr-11 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some initial review comments

@gavinking

Copy link
Copy Markdown
Member Author

Thanks @njr-11, should all be fixed now.

By the way, we need to check we're not using List.of() anywhere in Jakarta Data.

I didn't know about this can't-contain-null-elements thing until recently. What a landmine that is.

@njr-11 njr-11 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few additional comments,

Comment on lines +140 to +142
for (int i = 0; i < elements.size(); i++) {
final var element = elements.get(i);
parameters[i] = Parameter.of(element.getJavaType(), i, element.getAlias());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Suggested change
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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@gavinking
gavinking force-pushed the tuplestuff branch 2 times, most recently from c57cb0c to fb1c872 Compare October 20, 2026 08:19
parameters[i] =
Parameter.of(element.getJavaType(),
i+1, element.getAlias());
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's correct. That's what I was pointing out above:

An objection is that we really just create the Parameter to immediately throw it away. It would be easily possible to introduce an Argument type packaging a Parameter with a Tuple.Entry. The only issue with that is that to get it back out of the Tuple would 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:

  1. introduce some sort of Argument type, and probably unwind the integration with Tuple.
  2. double down on Tuple and just instantiate the Tuple.Entry instances directly.

Just not sure which direction to take this...

@njr-11 njr-11 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@gavinking
gavinking marked this pull request as draft October 20, 2026 21:37
@gavinking

Copy link
Copy Markdown
Member Author

ch05-metamodel-api.adoc has examples that no longer compile after the improvements in this PR and need to be updated:

Yes yes, I have not updated the spec yet. Sorry, I probably should have put the PR explicitly in draft mode.

@lucamolteni

Copy link
Copy Markdown
Contributor

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.

@gavinking

Copy link
Copy Markdown
Member Author

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 Parameters of the query, which is quite elegant.

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.

@gavinking
gavinking requested a review from njr-11 November 26, 2026 13:13
gavinking and others added 3 commits November 26, 2026 14:16
@gavinking

Copy link
Copy Markdown
Member Author

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 Tuples.

Please take a look @njr-11 @marko-bekhta @sebersole @lucamolteni

@gavinking
gavinking marked this pull request as ready for review November 26, 2026 13:26

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle null arguments in static queries

3 participants