1 16 package org.apache.cocoon.ojb.samples; 17 18 import javax.jdo.PersistenceManager; 19 import javax.jdo.Transaction; 20 21 import org.apache.cocoon.ojb.jdo.components.JDO; 22 import org.apache.cocoon.ojb.samples.bean.Employee; 23 import org.apache.ojb.broker.Identity; 24 import org.apache.ojb.broker.PersistenceBroker; 25 import org.apache.ojb.broker.PersistenceBrokerFactory; 26 27 33 public class EmployeeDAO { 34 35 public EmployeeDAO(){} 36 37 public void retrieve(Employee bean, JDO jdo) { 38 PersistenceManager persistenceManager = jdo.getPersistenceManager(); 40 41 Employee e = new Employee(); 42 e.setId(bean.getId()); 43 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 44 Identity oid = new Identity(e, broker); 45 46 Employee b = new Employee(); 47 persistenceManager.currentTransaction().begin(); 49 b = (Employee) persistenceManager.getObjectById(oid, false); 51 copyData(b, bean); 53 persistenceManager.currentTransaction().commit(); 55 } 56 57 public void insert(Employee e, JDO jdo) { 58 PersistenceManager persistenceManager = jdo.getPersistenceManager(); 60 Transaction tx = persistenceManager.currentTransaction(); 62 tx.begin(); 64 persistenceManager.makePersistent(e); 66 tx.commit(); 68 } 69 70 public void update(Employee bean, JDO jdo) { 71 PersistenceManager persistenceManager = jdo.getPersistenceManager(); 73 74 Employee e = new Employee(); 75 e.setId(bean.getId()); 76 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 77 Identity oid = new Identity(e, broker); 78 79 Employee b = new Employee(); 80 persistenceManager.currentTransaction().begin(); 82 b = (Employee) persistenceManager.getObjectById(oid, false); 84 copyData(bean, b); 86 persistenceManager.currentTransaction().commit(); 90 } 91 92 public void remove(Employee bean, JDO jdo) { 93 PersistenceManager persistenceManager = jdo.getPersistenceManager(); 95 96 Employee e = new Employee(); 97 e.setId(bean.getId()); 98 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 99 Identity oid = new Identity(e, broker); 100 101 Employee b = new Employee(); 102 persistenceManager.currentTransaction().begin(); 104 b = (Employee) persistenceManager.getObjectById(oid, false); 106 persistenceManager.deletePersistent(b); 108 persistenceManager.currentTransaction().commit(); 110 } 111 112 private void copyData(Employee from, Employee to) { 113 to.setId(from.getId()); 114 to.setDepartmentId(from.getDepartmentId()); 115 to.setName(from.getName()); 116 } 117 } 118 | Popular Tags |