Loading multiple instances by list of entity ids

From hibernate 5.1 we can load multiple instances of same class type in one shot by using the list of ids. The below code gives an example of this


import java.util.List;

import org.hibernate.MultiIdentifierLoadAccess;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import model.Series;

public class HibernateDemo {
    public static void main(String[] args) {
        SessionFactory sessionFactory = HibernateUtil.createSessionFactory();
        Session session = sessionFactory.openSession();

        MultiIdentifierLoadAccess<Series> multiIdentifierLoadAccess = session.byMultipleIds(Series.class);
        List series = multiIdentifierLoadAccess.multiLoad(1,2,3);
        for(Series obj : series) {
            System.out.println(obj.getId() + "\t" + obj.getName() + "\t" + obj.getDescription());
        }
        session.close();
        HibernateUtil.shutdown();
    }
}

In the code

session.byMultipleIds(Series.class);

returns an instance of MultiIdentifierLoadAccess interface which can be used to load multiple entities using the below function available in the interface

multiIdentifierLoadAccess.multiLoad(1,2,3);

Output

1 Monk Monk Description
2 Fringe Fringe Description
3 Bones Bones Description

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s