Skip to content
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

programmatic result set mappings for native queries #9

Open
lukasj opened this issue Dec 5, 2011 · 7 comments · May be fixed by #691
Open

programmatic result set mappings for native queries #9

lukasj opened this issue Dec 5, 2011 · 7 comments · May be fixed by #691

Comments

@lukasj
Copy link
Contributor

lukasj commented Dec 5, 2011

API for specifying result type mappings. Currently this is handled through metadata, and is therefore static

@lukasj
Copy link
Contributor Author

lukasj commented Dec 5, 2011

@glassfishrobot Commented
Reported by @ldemichiel

@lukasj
Copy link
Contributor Author

lukasj commented May 5, 2017

@glassfishrobot Commented
This issue was imported from java.net JIRA JPA_SPEC-9

@lukasj
Copy link
Contributor Author

lukasj commented Aug 31, 2018

@gavinking
Copy link
Contributor

Even though this is issue #9, from 2011, it's actually been quite near the top of my priority list for quite a long time.

We absolutely need an API-based way to define SQL result set mappings!

@gavinking
Copy link
Contributor

How about something like this:

Result.Entity<Order> orderResultMapping = 
        Result.entity(Order.class, 
                Result.field(Order_.id "order_id"),
                Result.field(Order_.quantity, "order_quantity"),
                Result.field(Order_.item, "order_item")
       );
Query<Order> query = entityManager.createNativeQuery("...",  orderResultMapping);

The class Result would have interfaces Entity<T>, Constructor<T>, Field<T>, and Column<T> with the same members as @EntityResult, @ConstructorResult, @FieldResult, and @ColumnResult. And it would have matching constructor methods entity(), constructor(), field(), and column().

There would also be:

  • a super interface Result.Typed<T> extended by these inner interfaces,
  • a Result.Compound extending Result.Typed<Object[]>, and perhaps even
  • a Result.Tuple extending Result.Typed<Tuple>.

Now, with use of static imports, the above code would simplify to:

Entity<Order> orderResultMapping = 
        entity(Order.class, 
                field(Order_.id "order_id"),
                field(Order_.quantity, "order_quantity"),
                field(Order_.item, "order_item")
       );
Query<Order> query = entityManager.createNativeQuery("...", orderResultMapping);

Hell, we could even in principle have Result.Entity<T> implement the annotation type EntityResult, Result.Field<T> implement the annotation type FieldResult, etc, which would mean that implementations would have almost no work to do.

@gavinking
Copy link
Contributor

On Twitter the idea of using string templates was suggested by Gunnar Morling.

I believe it would be possible to make something like this work:

NativeQuery query = 
        NATIVE."select o.id as \{Order_.id}, o.quantity as \{Order_.quantity}, o.item as \{Order_.item} from orders o";
Query<Order> query = entityManager.createNativeQuery(query);

Of course, something quite a bit more complicated would be needed to handle multiple instances of the same entity in the result set, embeddables, etc. And ultimately this would probably end up needing some sort of "dynamic" result set mapping API.

Now, I know that looks like a completely different feature to the one proposed in the issue description, but my intuition is that this is actually a better solution to the most common use cases for a result set mapping API.

Anyway, the point is that we should keep this application in mind in the design of a result set mapping API.

@gavinking gavinking added the candidate-for-4 Good candidate for JPA 4 label Aug 23, 2023
@gavinking gavinking linked a pull request Dec 20, 2024 that will close this issue
@gavinking gavinking changed the title Dynamic specification of result type mappings for native queries programmatic result set mappings for native queries Dec 20, 2024
@gavinking gavinking linked a pull request Dec 20, 2024 that will close this issue
@gavinking
Copy link
Contributor

See #691.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants