Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

API for creating SqlResultSetMappings #3879

gavinking started this conversation in Design Proposals
Discussion options

@maxandersen and apparently others are complaining that ResultTransformer has been deprecated with no current replacement. I tried telling him that getResultList().stream().map() is the replacement but I seem to have lost that argument.

The use case is native queries where I don't want to return or have to declare an @Entity class, and just want to package the columns up into attributes of a class.

So what I think we could do, that would avoid the need to introduce a whole new conceptual "construct" would be to provide a nice API for creating SqlResultSetMappings in code. Currently they can only be defined by the annotation, which I can imagine is often inconvenient, but they already let you declare a ConstructorResult which is the thing you need in Max's use case.

As an additional nice feature, I think it would also be fairly straightforward to let you define EmbeddableResults for the case where you do want to map properties, but don't have something that's truly an entity. That's not supported by the JPA annotation, but I think we could support it in Hibernate.

So the code would be something like, well, I dunno:

session.createNativeQuery("select firstName,lastName from person", new ConstructorResult(Name.class)).getResultList()

But of course more complicated things would be possible, essentially anything that's possible via @SqlResultSetMapping today.

A slightly more complicated example:

session.createNativeQuery("select firstName,lastName from person", new ConstructorResult(Name.class, "firstName", "lastName")).getResultList()
You must be logged in to vote

Replies: 3 comments 8 replies

Comment options

https://trello.com/c/KVV4Smqo/142-sql-resultset-mapping-builder

I'm not sure though in retrospect whether allowing all types of results make sense there.

You must be logged in to vote
1 reply
Comment options

We could also consider an alternative that uses Antlr to parse a String representation of what you want returned, similar to what we did for entity graphs

Comment options

trello.com/c/KVV4Smqo/142-sql-resultset-mapping-builder gives me a 404 ?

To be clear what I'm after is in the end not just raw classes, but mechanism to return records, map or even a json with just minimal code. in majority of cases where you are not after managed entities resultsetTransfomers worked great. This even makes sense on hql queries.

You must be logged in to vote
4 replies
Comment options

We'll agree to disagree wrt HQL. HQL already has VERY strong and fluent ways to do this

Comment options

And yeah, ResultTransformer was "nice" but also extremely problematic on the back-side. The newer split is significantly better

Comment options

I'm not saying hql is bad.

I'm saying often you don't need to nor want to be required to specify what is just as easily extracted from jdbc metadata from a sql query.

Comment options

I'm saying often you don't need to nor want to be required to specify what is just as easily extracted from jdbc metadata from a sql query.

I'll assume you mean specifically accessing the JDBC metadata for the "sql query" specifically relative to native-query... Because that's the only time Hibernate itself is not generating the SQL and we know the selections (we just generated them).

So given that, what metadata are you meaning? When?

Comment options

Allow programmatic definition of ResultSet mapping for use with native and procedure queries.

E.g.:

SessionFactory sf = ...;
ResultSetMappingBuilder builder = sf.getResultSetMappingBuilder();
ResultSetMapping mapping = builder.mapping();
// SQL : select id, subject, description from Issue issue
BasicResult id = builder.basicResult( "id", Integer.class );
BasicResult subject = builder.basicResult( "subject", String.class );
BasicResult description = builder.basicResult( "description", String.class );
InstantiationResult dto = builder.instantiationResult( MyDTO.class, id, subject );
InstantiationResult dto2 = builder.instantiationResult( MyNestedDTO.class, dto, description );
mapping.addResult( dto2 );
...
You must be logged in to vote
3 replies
Comment options

Comment options

Is that the minimal code for typical mappings ? It looks a lot more verbose.

I'll need to give it a go. I assume this is what is there in h6 main/master ?

Comment options

No this is not yet implemented in 6. At the moment, 6 still has the #addEntity, etc legacy API.

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

AltStyle によって変換されたページ (->オリジナル) /