Convert list of identifiers to list of objects with ORM like Hibernate

Web applications typically send a list of identifiers to the server when selecting collection items. There may be an HTML form representing the course, and it will contain a list with all students of the semester. By selecting some students, they will be associated with the course. The server will receive a list of student IDs.

What is the best practice for converting this list of identifiers (primary keys in the database) to a list of domain objects with ORMs like Hibernate? I would like to avoid re-writing the same code for each domain class. Grails does something similar (but I don't know how).

+3
source share
1 answer

DAO

public <T extends IDomainObject> List<T> getAll(Class<T> type, List<Integer> ids) {
    return (List<T>) session.createCriteria(type).add(Restrictions.in("id", ids).list();
}

IDomainObject id.

+4

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


All Articles