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

em().persist(this) も cascade=CascadeType.ALL 属性で注釈が付けられた関係に自動的にカスケードされることは知っています。また、なぜ saveAndCascade() の呼び出しを使用するのですか?

ソースコードを見たのですが、よくわかりません。どんな助けでも大歓迎です。

4

0 に答える 0