jakartaee / persistence

https://jakartaee.github.io/persistence/
Other
191 stars 55 forks source link

streamline use of 'select new' with record types #420

Open gavinking opened 1 year ago

gavinking commented 1 year ago

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.

trajano commented 12 months ago

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

gavinking commented 12 months ago

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

trajano commented 12 months ago

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

gavinking commented 4 months ago

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.