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]