Answer the question
In order to leave comments, you need to log in
What is the correct approach to initializing a Hibernate session in a Java web application?
I'm starting to delve into the Hibernate library and use it in a Java web application, the servlet accepts commands and actually processes them using Hibernate.
Here is a simple and small example:
Configuration cfg = new Configuration().configure().addAnnotatedClass(User.class);
ServiceRegistry reg = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
SessionFactory sf = cfg.buildSessionFactory(reg);
Session session = sf.openSession();
Transaction tr = session.beginTransaction();
session.save(user);
tr.commit();
session.close();
sf.close();
Answer the question
In order to leave comments, you need to log in
Not worth it.
And in general, it's better to read the Spring source code.
IMHO, everything is sensibly arranged there. It can be used not only as a tool but also as a guide.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question