-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Detaching an object
PostPosted: Sun Jul 20, 2008 11:31 pm 
Beginner
Beginner

Joined: Sun Oct 14, 2007 7:29 pm
Posts: 23
Hello,

I need to detach an object without closing the Session in order to modify a value in the object without persisting the change, but I can't seem to do this without closing the Session. I am using the being transaction and committ Hibernate methods, but even after the committ, the object returned from Hibernate is still persistent.

The code is below. I have a stateless session bean calling a UsrprofileDAO object to retrieve a user's profile. The UsrprofileDAO class retrieves the Hibernate Session from the HibernateUtil static class, starts a new transaction, queries the requested object, and committs the transaction. The DAO returns the Usrprofile object to the calling EJB Session bean where I mask the password field with x's ("xxxxxxxx") but I see that after I do this, the "xxxxxxxx" is persisted back to the database.

What I need to do is to detach the Usrprofile object before masking the password without closing the Hibernate Session because I then have to use the Hibernate Session in a CompdataDAO object. I am using the HibernateUtil that is recommended by the Hibernate Reference to get the Session object (code below).

My question is, how can I detach the Usrprofile object without closing the Hibernate Session? Alternatively, how can I close the Hibernate Session within the same EJB bean method (thread), mask the password, and then retrieve the Hibernate Session?


Code that retrieves the session and Usrprofile object:
Code:
   public com.faweb.entities.Usrprofiles getUsrProf(String strQueryStr, String username)  {
       myLogger = Logger.getLogger("com.faweb.entities.daos.UsrprofileDAO");
       myLogger.debug("getUsrProf input parameter username = " + username);
       usrProf = null;   
      
      try {
          myLogger.debug("Getting current Hibernate session object");         
          myLogger.debug("Getting Hibernate session object");
         Session session = HibernateUtil.getSessionFactory().getCurrentSession();
         
          session.beginTransaction();
         myLogger.debug("In UsrprofileDAO.getUsrProf(username), creating query");
           Query q = session.createQuery(strQueryStr); 
         myLogger.debug("In UsrprofileDAO.getUsrProf(username), query created");
           q.setString("username", username);
         myLogger.debug("In UsrprofileDAO.getUsrProf(username), set string");
         usrProf = (Usrprofiles) q.uniqueResult();
         myLogger.debug("In UsrprofileDAO.getUsrProf(username), got user " + usrProf);
         myLogger.debug("Committing Hibernate transaction");
            session.getTransaction().commit();
                   
         myLogger.debug(" Returning Usrprofile object");
         return usrProf;
   
      }



HibernanteUtil.java:
Code:
public class HibernateUtil {
    static Logger logger = Logger.getLogger("com.entities.utils.HibernateUtil");

    private static final SessionFactory sessionFactory;
    static {
        try {
        //    Create the SessionFactory from hibernate.cfg.xml
            // get a logger instance named "com.foo"

        //    System.out.println("In HibernateUtil.SessionFactor, about to try to create a SessionFactory instance.");
            sessionFactory = new Configuration().configure().buildSessionFactory();
            if(logger.isEnabledFor(Level.INFO))
               logger.info("Created Session Factory static class");

        } catch (Throwable ex) {
        //    Make sure you log the exception, as it might be swallowed
            logger.error("Initial SessionFactory creation failed." + ex);
            //System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    public static SessionFactory getSessionFactory() {
        try {
            logger.info("Looking up sessionFactory in JNDI in HibernateUtil.getSessionFactory()");
              javax.naming.InitialContext ctx = new javax.naming.InitialContext();
            logger.info("Got intiial JNDI context in HibernateUtil.getSessionFactory()");
           SessionFactory sf = (SessionFactory) ctx.lookup("appweb/HibernateSessionFactory");
           logger.info("Got SessionFactory in HibernateUtil.getSessionFactory()");
           return sf;
        }
        catch (Exception e)  {
            logger.error("threw exception in HibernateUtil.getSessionFactory().  Exception is " + e);
           return null;
        }
        //MCB -- Use to just return following object before Jan 13th, 2008
        //     return sessionFactory;
    }
}
[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 21, 2008 6:55 am 
Beginner
Beginner

Joined: Wed Sep 21, 2005 8:18 am
Posts: 31
There are three ways to detach an object

1- Session.close

2- Writing objects on any stream e.g. to File or on network

3- Cloning of object. See link below to clone any object
http://javainnovations.blogspot.com/2008/06/cloning-of-java-objects.html

_________________
amer sohail


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.