I am quite new in Spring world and I am trying to implement a simple Hibernate DAO but I have some doubts with delete and update operation
I am using Spring 3.2.1 and Hibernate 4.1.9 and MySQL database.
So in a MySQL database I have a table named person having the following structure:
mysql> describe person;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| pid | int(11) | NO | PRI | NULL | auto_increment |
| firstname | varchar(255) | YES | | NULL | |
| lastname | varchar(255) | YES | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
In my Spring application I have defined the following interface for my DAO that defined the required CRUD operation:
package org.andrea.myexample.HibernateOnSpring.dao;
import java.util.List;
import org.andrea.myexample.HibernateOnSpring.entity.Person;
public interface PersonDAO {
public void addPerson(Person p);
public Person getById(int id);
public List<Person> getPersonsList();
public void delete(int id);
public void update(Person person);
}
Then I have implement this interface by the class PersonDAOImplement in this way:
package org.andrea.myexample.HibernateOnSpring.dao;
import java.util.List;
import org.andrea.myexample.HibernateOnSpring.entity.Person;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
public class PersonDAOImpl implements PersonDAO {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
// Metodo che inserisce un nuovo record nella tabella person
@Transactional(readOnly = false)
public void addPerson(Person p) {
Session session = sessionFactory.openSession();
session.save(p);
session.close();
}
/*
* Metodo che recupera un record, rappresentante una persona, avente uno
* specifico id dalla tabella.
*
* @param L'id univoco della persona
*/
public Person getById(int id) {
Session session = sessionFactory.openSession();
try {
return (Person) session.get(Person.class, id);
} finally {
session.close();
}
}
/*
* Metodo che recupera la lista di tutti le persone rappresentanti dalle
* righe della tabella person
*/
@SuppressWarnings("unchecked")
public List<Person> getPersonsList() {
Session session = sessionFactory.openSession();
try {
Criteria criteria = session.createCriteria(Person.class);
return criteria.list();
} finally {
session.close();
}
}
/*
* Metodo che elimina dalla tabella person la riga avente uno specifico id
*
* @param l'id della persona da eliminare dalla tabella person
*/
@Transactional
public void delete(int id) {
Person personToDelete = getById(id);
sessionFactory.getCurrentSession().delete(personToDelete);
/*Session session = sessionFactory.openSession();
try {
Person personToDelete = getById(id);
System.out.println("person to delete: " + personToDelete);
session.delete(personToDelete);
} finally {
session.close();
}
*/
}
@Transactional
public void update(Person person){
sessionFactory.getCurrentSession().update(person);
/*
Session session = sessionFactory.openSession();
try {
System.out.println("UPDATING");
session.merge(person);
} finally {
System.out.println("CLOSE SESSION");
session.close();
}
*/
}
}
This example seems to work fine (I have tested it using a main class containing a main method in which I execute the CRUD method to insert row in the table, query for a single row or a list of row, delete a row and update the value in a row)
The only thing that I find strange is that to work correctly in the delete and update method I have to get the current session from my sessionFactory object in this way:
sessionFactory.getCurrentSession().delete(personToDelete);
sessionFactory.getCurrentSession().update(person);
On the contrary when I have to add or query a row I have to open a new session, in this way:
Session session = sessionFactory.openSession();
Why?
In the previous PersonDAOImpl class I have commented the old implementation of delete and update method in wich I tried to open a new session (as I do without problem in the addPerson and query method) but in this way don't work...work fine only if I get the current session
Why? Is it correct this implementation of DAO object?