001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest.spring.bean;
006:
007: import org.springframework.transaction.TransactionStatus;
008: import org.springframework.transaction.support.DefaultTransactionDefinition;
009:
010: import com.tctest.spring.bean.domain.PersistentObj;
011: import com.tctest.spring.bean.orm.hibernate.HibernateDAO;
012:
013: public class HibernateBean implements IHibernateBean {
014: private Object sharedOne = null;
015: private transient HibernateDAO dao = null;
016:
017: public void associateSharedObj() {
018: dao.getHibernateTemplate().update(this .getSharedOne());
019: }
020:
021: public void sharePersistentObj() {
022: TransactionStatus status = dao.getTransactionManager()
023: .getTransaction(new DefaultTransactionDefinition());
024: PersistentObj po = new PersistentObj();
025: po.setStrFld("persistent");
026: dao.getHibernateTemplate().save(po);
027: this .setSharedOne(po); // share persistent object
028: dao.getTransactionManager().commit(status);
029: }
030:
031: public void shareDetachedObj() {
032: TransactionStatus status = dao.getTransactionManager()
033: .getTransaction(new DefaultTransactionDefinition());
034: PersistentObj po = new PersistentObj();
035: po.setStrFld("detached");
036: dao.getHibernateTemplate().save(po);
037: dao.getTransactionManager().commit(status);
038: this .setSharedOne(po); // share detched object
039: }
040:
041: public void shareLazyObj() {
042: Integer id = null;
043: PersistentObj po = new PersistentObj();
044: po.setStrFld("parent");
045: PersistentObj child = new PersistentObj();
046: child.setStrFld("lazy");
047: po.setChild(child);
048: dao.getHibernateTemplate().save(po);
049: id = po.getId();
050:
051: TransactionStatus status = dao.getTransactionManager()
052: .getTransaction(new DefaultTransactionDefinition());
053: po = (PersistentObj) dao.getHibernateTemplate().get(
054: PersistentObj.class, id);
055: this .setSharedOne(po.getChild()); // share proxied object
056: dao.getTransactionManager().commit(status);
057: }
058:
059: public void shareObjWithLazyChild() {
060: Integer id = null;
061: PersistentObj po = new PersistentObj();
062: po.setStrFld("parent");
063: PersistentObj child = new PersistentObj();
064: child.setStrFld("lazy");
065: po.setChild(child);
066: dao.getHibernateTemplate().save(po);
067: id = po.getId();
068:
069: TransactionStatus status = dao.getTransactionManager()
070: .getTransaction(new DefaultTransactionDefinition());
071: po = (PersistentObj) dao.getHibernateTemplate().get(
072: PersistentObj.class, id);
073: this .setSharedOne(po); // share proxied object
074: dao.getTransactionManager().commit(status);
075: }
076:
077: public Integer getSharedId() {
078: PersistentObj persistentObj = (PersistentObj) getSharedOne();
079: return persistentObj == null ? null : persistentObj.getId();
080: }
081:
082: public String getSharedFld() {
083: PersistentObj persistentObj = (PersistentObj) getSharedOne();
084: return persistentObj == null ? null : persistentObj.getStrFld();
085: }
086:
087: public HibernateDAO getDao() {
088: return dao;
089: }
090:
091: public void setDao(HibernateDAO dao) {
092: this .dao = dao;
093: }
094:
095: synchronized public Object getSharedOne() {
096: // System.err.println("----" + this.sharedOne.getClass().getName());
097: return sharedOne;
098: }
099:
100: synchronized public void setSharedOne(Object sharedOne) {
101: this.sharedOne = sharedOne;
102: }
103:
104: }
|