N
N
Nikita K2019-12-25 12:00:11
Spring
Nikita K, 2019-12-25 12:00:11

How to return a list of objects in spring?

There is an entity Ship, it has 7 fields. Condition: return a list of all objects using a get request, by default there can be 3 entities on the page, or, if specified, then something else. Can you at least give an example of how to deal with this? I can return only 1 entity through model by putting it there. I don't know how many to put in...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2019-12-25
@yung_filosof

To thrust in model List of entities.

M
Maxim Fedorov, 2019-12-25
@Maksclub

@Repository
class ShipRepository {
 
    @PersistenceContext
    private EntityManager entityManager;
 
    @Override
    public List<Ship> findLimitedTo(int limit) {
        return entityManager.createQuery("SELECT s FROM Ship s ORDER BY s.id",
          Ship.class).setMaxResults(limit).getResultList();
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question