Dear Hibernate users,
I'm going to replace a JDBC layer with Hibernate. I have tested the basic Hibernate examples now I'd like to know if I can avoid istantiating all Hibernate objects for every query/insert.
For example:
Is it good practice to put in a Singleton the
SessionFactory and
Configuration
Code:
public getInstance() {
if (config == null) {
config = new Configuration();
}
if (sessionFactory == null) {
sessionFactory = config.buildSessionFactory();
}
}
Also, is there any advantage to keeping the Session object at Class level so that I don't open a new one for every query ?
Code:
public void getConnection {
if (session == null) {
session = sessionFactory.openSession();
}
else {
return session;
}
}
Thanks for your help
Francesco