在Play框架1.2.x中,类JPABase分析,为什么它使用调用saveAndCascade()?

时间:2013-10-28 13:54:02

标签: jpa playframework-1.x

 public void _save() {
        if (!em().contains(this)) {
            em().persist(this);
            PlayPlugin.postEvent("JPASupport.objectPersisted", this);
        }
        avoidCascadeSaveLoops.set(new ArrayList<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());
            } else {
                throw e;
            }
        }
        avoidCascadeSaveLoops.set(new ArrayList<JPABase>());
        try {
            saveAndCascade(false);
        } finally {
            avoidCascadeSaveLoops.get().clear();
        }
    }


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);
    }
}

为什么使用调用saveAndCascade()? 我查看了源代码,但我无法理解。 任何帮助表示赞赏。

我想学习如何使用playframework,但是我无法理解它。我阅读了Play + Framework + Cookbook和Wayne Ellis的书籍 - 介绍Play框架,但是我找不到关于播放源的分析。 任何书籍,文档或声明都表示赞赏。

1 个答案:

答案 0 :(得分:0)

在正常的JPA环境中,EntityManager.flush()方法将检查所有已加载的实体,如果它们被修改 - 更改将保存回数据库。

Play-1的工作方式不同 - 此函数设置willBeSaved变量,稍后在Hibernate拦截器上检查它并仅允许更新此特定实体,因此如果有其他修改过的实体 - 它们不会更新。