Note that there are some explanatory texts on larger screens.

plurals
  1. POSome doubts about delete and updating table row using Hibernate in Spring application
    primarykey
    data
    text
    <p>I am quite new in Spring world and I am trying to implement a simple Hibernate DAO but I have some doubts with <strong>delete</strong> and <strong>update operation</strong> </p> <p>I am using Spring 3.2.1 and Hibernate 4.1.9 and MySQL database.</p> <p>So in a MySQL database I have a table named <strong>person</strong> having the following structure:</p> <pre><code>mysql&gt; 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 | | +-----------+--------------+------+-----+---------+----------------+ </code></pre> <p>In my Spring application I have defined the following interface for my DAO that defined the required CRUD operation:</p> <pre><code>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&lt;Person&gt; getPersonsList(); public void delete(int id); public void update(Person person); } </code></pre> <p>Then I have implement this interface by the class <strong>PersonDAOImplement</strong> in this way:</p> <pre><code>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&lt;Person&gt; 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(); } */ } } </code></pre> <p>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)</p> <p>The only thing that I find strange is that to work correctly in the <strong>delete</strong> and <strong>update</strong> method I have to <strong>get the current session from my sessionFactory object</strong> in this way:</p> <pre><code>sessionFactory.getCurrentSession().delete(personToDelete); sessionFactory.getCurrentSession().update(person); </code></pre> <p>On the contrary when I have to <strong>add or query</strong> a row I have to <strong>open a new session</strong>, in this way:</p> <pre><code>Session session = sessionFactory.openSession(); </code></pre> <p>Why?</p> <p>In the previous <strong>PersonDAOImpl</strong> class I have commented the old implementation of <strong>delete</strong> and <strong>update</strong> 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</p> <p>Why? Is it correct this implementation of DAO object?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload