0

I have a large project sitting on top of a large database with multiple tables. Data gets inserted, updated, queried (including joins) and deleted. Up to and including Hibernate 6, I've been using createNativeQuery for some of these operations, particularly when the returned results are complex (e.g. "select a.something,b.somethingelse from table1 a left join table 2 b on (b.thisfield=a.thatfield) where (a.anotherfield in (select anotherfield from table3 where somedate>=:adate))")

Up until now, I could do that just by calling .createNativeQuery(sqlstring), set the parameters, execute and then read the results using ResultSet and .getInt, .getString etc.

Now, apparently, I have to supply a class - yet I'm not actually wanting to create a new table, this is a set of fields from different tables! How on earth do I do what I'm trying to do?

This is a simple example - the real queries are far more complex, and there are lots of them... All the examples I find online are either for ridiculously simple single table cases (e.g. .createNativeQuery("select * from customers",Customer.class)) or date back years to when just .createNativeQuery(sqlstring) was still allowed.

(Additionally, I'd like to know what class I should be using when the query doesn't actually return anything anyway, e.g. a delete from... or update... set... where...)

Thanks for any pointers!

6
  • You don't need an entity it can be any class just a basic DTO to map your results to. See docs.jboss.org/hibernate/orm/7.0/javadocs/org/hibernate/query/… Commented Oct 8 at 11:45
  • As for the delete/updates you can use createNativeMutationQuery in the same documentation from @M.Deinum above. Commented Oct 8 at 15:41
  • Thanks @JorgeCampos - I didn't know about that, that will make the changeover a lot easier. Commented Oct 10 at 13:44
  • @M.Deinum, it still sounds like I need to create DTOs for each of the remaining few dozen queries, probably a different DTO for each one. Whereas previously, I could just call some SQL... I started using Hibernate as it was supposed to make life easier - it certainly does when it's just single table operations, but for anything more complex, it's now significantly harder! Commented Oct 10 at 13:44
  • What did you do with the result of the queries? Ignore it? Commented Oct 13 at 5:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.