0

In mybatis we can define very complex mapping of the sql result set to an arbitrary complex dto object. Take a look at the example here:

http://www.mybatis.org/mybatis-3/sqlmap-xml.html

at the section "Advanced Result Maps" where we have a mapper for the Blog which has single author and the collection of posts and every post has a collection of comments and tags.

The question is: Is it technically possible and how in JPA/Hibernate to create SqlResultSetMapping that maps native sql query like this in previous example to the dto object like this (Blog) in previous example? The assumption is that the Blog is not part of our entity model but some dto object for the specific use case. If this can't be achieved by using SqlResultSetMapping what is the best way to achieve this in JPA/Hibernate under the assumption that Blog, Post, Comment... are some specific dto objects for the specific use case?

Please keep in mind that this is only the example for illustration and that focus in on "is this technically possible and how" and not on "why should someone do such a thing in this example when you can define JPA relations and let framework generate sql for you...".

Mladen Krstić
  • 188
  • 3
  • 11
  • Sure it's possible. And now - what you are going to do with this answer? – Simon Martinelli Sep 29 '18 at 15:00
  • Ok, question corrected. Thanks for suggestion. :) – Mladen Krstić Sep 29 '18 at 15:10
  • No that's not possible because the result of a SQL result set mapping cannot contain collection. But this can be easily done with entity mapping. You can also use them read only. And you should not compare myBatis with sql result set mapping but with O/R mapping in general. – Simon Martinelli Sep 29 '18 at 15:19
  • Thank you. So SqlResultSetMapping is not an option. It's perfectly clear that this can be easily done with entity mapping, but this question is under assumption that Blog, Author, Post, Comment and Tag are some very specific dto objects for the very specific use case and are not part of the actual entity model. If I understood you correctly, you suggest to declare Comment as an entity but make it read only and do the same thing with Post and then make OneToMany relation between the read only Post and the read only Comment? – Mladen Krstić Sep 29 '18 at 15:38

0 Answers0