예제 #1
0
파일: JPABase.java 프로젝트: novayoung/play
 public void _save() {
   if (!em().contains(this)) {
     em().persist(this);
     PlayPlugin.postEvent("JPASupport.objectPersisted", this);
   }
   avoidCascadeSaveLoops.set(new HashSet<JPABase>());
   try {
     saveAndCascade(true);
   } finally {
     avoidCascadeSaveLoops.get().clear();
   }
   try {
     em().flush();
   } catch (PersistenceException e) {
     if (e.getCause() instanceof GenericJDBCException) {
       throw new PersistenceException(((GenericJDBCException) e.getCause()).getSQL(), e);
     } else {
       throw e;
     }
   }
   avoidCascadeSaveLoops.set(new HashSet<JPABase>());
   try {
     saveAndCascade(false);
   } finally {
     avoidCascadeSaveLoops.get().clear();
   }
 }
예제 #2
0
파일: JPABase.java 프로젝트: novayoung/play
 public void _delete() {
   try {
     avoidCascadeSaveLoops.set(new HashSet<JPABase>());
     try {
       saveAndCascade(true);
     } finally {
       avoidCascadeSaveLoops.get().clear();
     }
     em().remove(this);
     try {
       em().flush();
     } catch (PersistenceException e) {
       if (e.getCause() instanceof GenericJDBCException) {
         throw new PersistenceException(((GenericJDBCException) e.getCause()).getSQL(), e);
       } else {
         throw e;
       }
     }
     avoidCascadeSaveLoops.set(new HashSet<JPABase>());
     try {
       saveAndCascade(false);
     } finally {
       avoidCascadeSaveLoops.get().clear();
     }
     PlayPlugin.postEvent("JPASupport.objectDeleted", this);
   } catch (PersistenceException e) {
     throw e;
   } catch (Throwable e) {
     throw new RuntimeException(e);
   }
 }
예제 #3
0
파일: JPABase.java 프로젝트: novayoung/play
 private void saveAndCascade(boolean willBeSaved) {
   this.willBeSaved = willBeSaved;
   if (avoidCascadeSaveLoops.get().contains(this)) {
     return;
   } else {
     avoidCascadeSaveLoops.get().add(this);
     if (willBeSaved) {
       PlayPlugin.postEvent("JPASupport.objectUpdated", this);
     }
   }
   // Cascade save
   try {
     Set<Field> fields = new HashSet<Field>();
     Class clazz = this.getClass();
     while (!clazz.equals(JPABase.class)) {
       Collections.addAll(fields, clazz.getDeclaredFields());
       clazz = clazz.getSuperclass();
     }
     for (Field field : fields) {
       field.setAccessible(true);
       if (Modifier.isTransient(field.getModifiers())) {
         continue;
       }
       boolean doCascade = false;
       if (field.isAnnotationPresent(OneToOne.class)) {
         doCascade = cascadeAll(field.getAnnotation(OneToOne.class).cascade());
       }
       if (field.isAnnotationPresent(OneToMany.class)) {
         doCascade = cascadeAll(field.getAnnotation(OneToMany.class).cascade());
       }
       if (field.isAnnotationPresent(ManyToOne.class)) {
         doCascade = cascadeAll(field.getAnnotation(ManyToOne.class).cascade());
       }
       if (field.isAnnotationPresent(ManyToMany.class)) {
         doCascade = cascadeAll(field.getAnnotation(ManyToMany.class).cascade());
       }
       if (doCascade) {
         Object value = field.get(this);
         if (value == null) {
           continue;
         }
         if (value instanceof PersistentMap) {
           if (((PersistentMap) value).wasInitialized()) {
             for (Object o : ((Map) value).values()) {
               if (o instanceof JPABase) {
                 ((JPABase) o).saveAndCascade(willBeSaved);
               }
             }
           }
           continue;
         }
         if (value instanceof PersistentCollection) {
           if (((PersistentCollection) value).wasInitialized()) {
             for (Object o : (Collection) value) {
               if (o instanceof JPABase) {
                 ((JPABase) o).saveAndCascade(willBeSaved);
               }
             }
           }
           continue;
         }
         if (value instanceof HibernateProxy && value instanceof JPABase) {
           if (!((HibernateProxy) value).getHibernateLazyInitializer().isUninitialized()) {
             ((JPABase) ((HibernateProxy) value).getHibernateLazyInitializer().getImplementation())
                 .saveAndCascade(willBeSaved);
           }
           continue;
         }
         if (value instanceof JPABase) {
           ((JPABase) value).saveAndCascade(willBeSaved);
           continue;
         }
       }
     }
   } catch (Exception e) {
     throw new UnexpectedException("During cascading save()", e);
   }
 }