Return-Path: Delivered-To: apmail-jakarta-turbine-jcs-dev-archive@www.apache.org Received: (qmail 96603 invoked from network); 9 Aug 2004 20:52:13 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 9 Aug 2004 20:52:13 -0000 Received: (qmail 78206 invoked by uid 500); 9 Aug 2004 20:52:12 -0000 Delivered-To: apmail-jakarta-turbine-jcs-dev-archive@jakarta.apache.org Received: (qmail 78177 invoked by uid 500); 9 Aug 2004 20:52:12 -0000 Mailing-List: contact turbine-jcs-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Turbine JCS Developers List" Reply-To: "Turbine JCS Developers List" Delivered-To: mailing list turbine-jcs-dev@jakarta.apache.org Received: (qmail 78159 invoked by uid 99); 9 Aug 2004 20:52:12 -0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received: from [69.28.191.194] (HELO exchange.ifilm) (69.28.191.194) by apache.org (qpsmtpd/0.27.1) with ESMTP; Mon, 09 Aug 2004 13:52:08 -0700 Message-ID: From: Travis Savo To: 'Turbine JCS Developers List' Subject: JCSWorker extension for Hibernate Date: Mon, 9 Aug 2004 13:52:12 -0700 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N Here's an example of how JCSWorker can be extended to reassociate objects with a Hibernate session. This is absolutely mandatory when using Hibernate with Remote Cache and lazy loaded collections because without doing this, lazy loaded collections won't be able to find the session to reload themselves with. Where does this type of stuff belong, Aaron? Is there a application specific package things like this belong in? I have another... a OSCache->JCS bridge, which has proven quite useful. import java.io.Serializable; import java.util.Iterator; import java.util.List; import net.sf.hibernate.LockMode; import net.sf.hibernate.Session; import org.apache.jcs.utils.access.JCSWorker; import org.apache.jcs.utils.access.JCSWorkerHelper; import org.apache.log4j.Logger; import org.apache.log4j.Priority; public class HibernateJCSWorker extends JCSWorker { private static final Logger logger = Logger.getLogger(HibernateJCSWorker.class); public HibernateJCSWorker(String aName) { super(aName); } /** * @deprecated You should be using getResult(Serializable aKey, JCSWorkerHelper aWorker, Session aSession) throws Exception { */ public Object getResult(Serializable aKey, JCSWorkerHelper aWorker) throws Exception{ return getResult(aKey, null, aWorker, null); } /** * @deprecated You should be using getResult(Serializable aKey, String aGroup, JCSWorkerHelper aWorker, Session aSession) throws Exception { */ public Object getResult(Serializable aKey, String aGroup, JCSWorkerHelper aWorker) throws Exception{ return getResult(aKey, aGroup, aWorker, null); } /** * Wrapper on JCSWorker which takes a session to reassociate objects with. */ public Object getResult(Serializable aKey, JCSWorkerHelper aWorker, Session aSession) throws Exception { return getResult(aKey, null, aWorker, aSession); } /** * Wrapper on JCSWorker which takes a session to reassociate objects with. */ public Object getResult(Serializable aKey, String aGroup, JCSWorkerHelper aHelper, Session aSession) throws Exception { Object result = super.getResult(aKey, aGroup, aHelper); if(aSession == null){ return result; } try { if (result instanceof List) { for (Iterator i = ((List) result).iterator(); i.hasNext();) { Object o = i.next(); reassociate(o, aSession); } } else { reassociate(result, aSession); } } catch (Exception e) { if (logger.isEnabledFor(Priority.INFO)) { logger.info("Couldn't reassociate a session with a " + result); logger.info(e); } } return result; } /** * Reassociates an object with a session so lazy loaded collections will still work. */ public void reassociate(Object anObject, Session aSession) throws Exception { if (!aSession.contains(anObject)) { aSession.lock(anObject, LockMode.NONE); } } } -Travis Savo --------------------------------------------------------------------- To unsubscribe, e-mail: turbine-jcs-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: turbine-jcs-dev-help@jakarta.apache.org