- 
 
 - 
  Notifications
 
You must be signed in to change notification settings  - Fork 3.7k
 
API for creating SqlResultSetMappings #3879
-
@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()
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 3 comments 8 replies
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
We'll agree to disagree wrt HQL. HQL already has VERY strong and fluent ways to do this
Beta Was this translation helpful? Give feedback.
All reactions
-
And yeah, ResultTransformer was "nice" but also extremely problematic on the back-side. The newer split is significantly better
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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?
Beta Was this translation helpful? Give feedback.
All reactions
-
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 );
...
Beta Was this translation helpful? Give feedback.
All reactions
- 
 
👍 1 
-
Funny, this looks a lot like projections in Hibernate Search 6: https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-dsl-projection-composite
Beta Was this translation helpful? Give feedback.
All reactions
-
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 ?
Beta Was this translation helpful? Give feedback.
All reactions
-
No this is not yet implemented in 6. At the moment, 6 still has the #addEntity, etc legacy API.
Beta Was this translation helpful? Give feedback.