I\'m trying to integrate spring and Hibernate with mysql. I created a simple java project and a package with 3 classes, an application context.xml file, and an hbm.xml for mappi
If you are using Spring 4+, you should use SessionFactory
. Also, you need these jars: commons-pool, commons-dbcp.
This is a sample code that I made, I hope it helps you:
public class EmployeeDAOImpl implements EmployeeDAO{
private SessionFactory sessionFactory;
@Override
public void saveEmployee(Employee e) {
Session session = this.sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.persist(e);
tx.commit();
session.close();
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
applicationContext.xml
:
employee.hbm.xml
org.hibernate.dialect.Oracle9Dialect
update
true