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 - Introducing the Play Framework这本书,但我找不到一些关于play源码的分析。任何书籍、文档或声明都会受到赞赏。