facebook

How Hibernate DAO Close the session ?

  1. MyEclipse Archived
  2.  > 
  3. Examples On-Demand
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #314724 Reply

    Jainitya
    Member

    Hi Friends,

    I’m new to this forum so kindly bear with me…

    I’ve a confusion regarding use of Hibernate with Myeclipse… I’m seeing that while executing my web-application hibernate opens a session with the help of getSession() but in no place (so far) I’ve seen how Hibernate closes this sesion… My question is how/or at which place session gets closed [I’m using default configuration of Hibernate with Myeclipse].
    Below is the code snipset of MyEclipse generated DAO.

    Thanks in advance
    package bean;

    import java.sql.Timestamp;
    import java.util.List;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.hibernate.LockMode;
    import org.hibernate.Query;
    import org.hibernate.criterion.Example;

    /**
    * A data access object (DAO) providing persistence and search support for
    * Selllog entities. Transaction control of the save(), update() and delete()
    * operations can directly support Spring container-managed transactions or they
    * can be augmented to handle user-managed Spring transactions. Each of these
    * methods provides additional information for how to configure it for the
    * desired type of transaction control.
    *
    * @see bean.Selllog
    * @author MyEclipse Persistence Tools
    */

    public class SelllogDAO extends BaseHibernateDAO {
    private static final Log log = LogFactory.getLog(SelllogDAO.class);
    // property constants
    public static final String OPENING_BALENCE = “openingBalence”;
    public static final String RECEIVE = “receive”;
    public static final String TRANSFER = “transfer”;
    public static final String TRANSFER_TO_COMMENT = “transferToComment”;
    public static final String DAY_BRAND_RATE = “dayBrandRate”;
    public static final String CLOSING_BALANCE = “closingBalance”;
    public static final String LAST_UPDATE_USE_ID = “lastUpdateUseId”;

    public void save(Selllog transientInstance) {
    log.debug(“saving Selllog instance”);
    try {
    getSession().save(transientInstance);
    log.debug(“save successful”);
    } catch (RuntimeException re) {
    log.error(“save failed”, re);
    throw re;
    }
    }

    public void delete(Selllog persistentInstance) {
    log.debug(“deleting Selllog instance”);
    try {
    getSession().delete(persistentInstance);
    log.debug(“delete successful”);
    } catch (RuntimeException re) {
    log.error(“delete failed”, re);
    throw re;
    }
    }

    public Selllog findById(java.lang.Integer id) {
    log.debug(“getting Selllog instance with id: ” + id);
    try {
    Selllog instance = (Selllog) getSession().get(“bean.Selllog”, id);
    return instance;
    } catch (RuntimeException re) {
    log.error(“get failed”, re);
    throw re;
    }
    }

    public List findByExample(Selllog instance) {
    log.debug(“finding Selllog instance by example”);
    try {
    List results = getSession().createCriteria(“bean.Selllog”).add(
    Example.create(instance)).list();
    log.debug(“find by example successful, result size: ”
    + results.size());
    return results;
    } catch (RuntimeException re) {
    log.error(“find by example failed”, re);
    throw re;
    }
    }

    public List findByProperty(String propertyName, Object value) {
    log.debug(“finding Selllog instance with property: ” + propertyName
    + “, value: ” + value);
    try {
    String queryString = “from Selllog as model where model.”
    + propertyName + “= ?”;
    Query queryObject = getSession().createQuery(queryString);
    queryObject.setParameter(0, value);
    return queryObject.list();
    } catch (RuntimeException re) {
    log.error(“find by property name failed”, re);
    throw re;
    }
    }

    public List findByOpeningBalence(Object openingBalence) {
    return findByProperty(OPENING_BALENCE, openingBalence);
    }

    public List findByReceive(Object receive) {
    return findByProperty(RECEIVE, receive);
    }

    public List findByTransfer(Object transfer) {
    return findByProperty(TRANSFER, transfer);
    }

    public List findByTransferToComment(Object transferToComment) {
    return findByProperty(TRANSFER_TO_COMMENT, transferToComment);
    }

    public List findByDayBrandRate(Object dayBrandRate) {
    return findByProperty(DAY_BRAND_RATE, dayBrandRate);
    }

    public List findByClosingBalance(Object closingBalance) {
    return findByProperty(CLOSING_BALANCE, closingBalance);
    }

    public List findByLastUpdateUseId(Object lastUpdateUseId) {
    return findByProperty(LAST_UPDATE_USE_ID, lastUpdateUseId);
    }

    public List findAll() {
    log.debug(“finding all Selllog instances”);
    try {
    String queryString = “from Selllog”;
    Query queryObject = getSession().createQuery(queryString);
    return queryObject.list();
    } catch (RuntimeException re) {
    log.error(“find all failed”, re);
    throw re;
    }
    }

    public Selllog merge(Selllog detachedInstance) {
    log.debug(“merging Selllog instance”);
    try {
    Selllog result = (Selllog) getSession().merge(detachedInstance);
    log.debug(“merge successful”);
    return result;
    } catch (RuntimeException re) {
    log.error(“merge failed”, re);
    throw re;
    }
    }

    public void attachDirty(Selllog instance) {
    log.debug(“attaching dirty Selllog instance”);
    try {
    getSession().saveOrUpdate(instance);
    log.debug(“attach successful”);
    } catch (RuntimeException re) {
    log.error(“attach failed”, re);
    throw re;
    }
    }

    public void attachClean(Selllog instance) {
    log.debug(“attaching clean Selllog instance”);
    try {
    getSession().lock(instance, LockMode.NONE);
    log.debug(“attach successful”);
    } catch (RuntimeException re) {
    log.error(“attach failed”, re);
    throw re;
    }
    }
    }

    #314732 Reply

    Jainitya,
    Please take a look at the FAQ section of the Hibernate tutorial – http://www.myeclipseide.com/images/tutorials/quickstarts/introduction_to_hibernate/tutorial.html .
    It briefly explains the Hibernate Session and various sessions management strategies. I would also recommend you to go through the tutorials to get an clear understanding of the Hibernate support provided by MyEclipse. A complete list of the tutorials can be found here – http://www.myeclipseide.com/module-htmlpages-display-pid-7.html

    #314733 Reply

    Jainitya
    Member

    Hi I have gone through it and still did not any answer from there , Let me define it again.
    what I have found in tutorial and books to save a Object we are using this code
    Session session = SessionFactoryHelper.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    session.save(label);
    session.getTransaction().commit();

    Now when I am looking at the DAO code it is clear to me that with the help of getSession() method we are geeting an instece of the session(this memthod is in Factry class

    but in hibernate genrated DAO class no where we are closing or commiting the session (getSession().close or getSession().commit )

    How and when er are doing this in DAO ?

Viewing 3 posts - 1 through 3 (of 3 total)
Reply To: How Hibernate DAO Close the session ?

You must be logged in to post in the forum log in