How to work with a list of objects after a Hibernate request for a single object

After calling list () in a sleep request, which you expect to just return a list of Foo objects (see example below), what is the best way to handle this situation?

Query query = session.createQuery("from Foo");
List list = query.list();

I especially don't like this:

public List read() { ... }

When I would prefer:

public List<Foo> read() { ... }

Do you expect calling method calls to the reader method to be added to Foo for each element? Is there a good way to force the read method to return a List <Foo>?

+3
source share
1 answer

Do you expect calling method calls to the reader method to be added to Foo for each element? Is there a good way to force the read method to return a List <Foo>?

No, I would not expect the caller to throw, so I would write things like this:

Query query = session.createQuery("from Foo");
List<Foo> list = query.list();

(API- Hibernate Query ):

Query query = session.createQuery("from Foo");
@SuppressWarnings("unchecked")
List<Foo> list = query.list();
+2

Source: https://habr.com/ru/post/1770641/


All Articles