1 17 18 package org.objectweb.jac.aspects.hibernate; 19 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import net.sf.hibernate.HibernateException; 24 import net.sf.hibernate.ObjectNotFoundException; 25 import net.sf.hibernate.Session; 26 27 import org.aopalliance.intercept.ConstructorInvocation; 28 import org.aopalliance.intercept.MethodInvocation; 29 import org.objectweb.jac.core.AspectComponent; 30 import org.objectweb.jac.core.Interaction; 31 import org.objectweb.jac.core.NameRepository; 32 import org.objectweb.jac.core.Wrapper; 33 import org.objectweb.jac.util.Repository; 34 35 41 public class BeginPersistentSessionWrapper extends Wrapper { 42 43 44 private HibernateHelper hh = HibernateHelper.get(); 45 46 public BeginPersistentSessionWrapper( AspectComponent ac ) { 47 super(ac); 48 } 49 50 54 public Object invoke( MethodInvocation invocation ) { 55 Interaction interaction = (Interaction) invocation; 56 try { 57 return _begin(interaction); 58 } catch (HibernateException e) { 59 e.printStackTrace(); 60 System.exit(1); 61 } 62 return null; 63 } 64 private Object _begin( Interaction interaction ) throws HibernateException { 65 66 hh.openSessionAndBeginTx(); 67 Session session = hh.getSession(); 68 69 74 HibernateAC hac = (HibernateAC) ac; 75 List pObjects = hac.getPersistentObjects(); 76 for ( Iterator iter=pObjects.iterator() ; iter.hasNext() ; ) { 77 78 String name = (String ) iter.next(); 79 Object object = names.getObject(name); 80 81 try { 82 session.load(object,name); 83 } catch (ObjectNotFoundException e) { 84 session.save(object,name); 85 } 86 } 87 88 91 return proceed(interaction); 92 } 93 94 95 98 private Repository names = NameRepository.get(); 99 100 public Object construct(ConstructorInvocation invocation) 101 throws Throwable { 102 throw new Exception ("This wrapper does not support constructor wrapping"); 103 } 104 } 105 | Popular Tags |