persistence icon indicating copy to clipboard operation
persistence copied to clipboard

streamline use of 'select new' with record types

Open gavinking opened this issue 2 years ago • 4 comments
trafficstars

I would like to propose that we let people write:

record BookSummary(String title, String author) {}
em.createQuery("select book.title, book.author.name from Book book", BookSummary.class).getResultList();

instead of forcing them into the extra ceremony of:

record BookSummary(String title, String author) {}
em.createQuery("select new BookSummary(book.title, book.author.name) from Book book", BookSummary.class).getResultList();

This isn't critical, of course, but it's a pretty easy simplification to the user experience.

gavinking avatar May 30 '23 13:05 gavinking

Why can't we have it for non-record types? Provided there is a single public constructor

trajano avatar Aug 09 '23 13:08 trajano

Yes, sure, it would also be allowed for non-records. Sorry for being unclear.

gavinking avatar Aug 10 '23 08:08 gavinking

Nothing to be sorry about @gavinking I presume the purpose of this repo is to discuss things to make sure things are clear and less behaviour is unspecified and vendor dependent

trajano avatar Aug 10 '23 13:08 trajano

Another thing to consider as part of this is to just do certain sorts of implicit type conversions, for example, allow:

int count = em.createSelectionQuery("select count(this) from Book", int.class).getSingleResult();

which would currently throw because count(this) is of type Long.

gavinking avatar Apr 05 '24 23:04 gavinking