I am trying to migrate to java 8 and have a number of methods in my dao classes which do the following
@Override
@SuppressWarnings("unchecked")
public List<Group> getGroups()
{
Session session = sessionFactory.openSession();
List<Group> allGroups = (List<Group>)session.createQuery("from Group").list();
session.close();
return allGroups;
}
Here the same boiler plate sessionFactory.open
and session.close
is repeated for all methods.
Is it possible in Java 8 to have a method which does the open and close and takes a function which is the rest of my code and execute it inbetween?
If so - what is the name of this process , or can anyone provide some help on how this might be achieved