manytoonefield.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,403 行 · 第 1/3 页
JAVA
1,403 行
//out.println(varName + ".__caucho_setLoadMask(" + varName + ".__caucho_getLoadMask(" + targetGroup + ") | " + targetMask + "L, " + targetGroup + ");"); varName = "((" + _targetType.getInstanceClassName() + ") " + varName + ")"; // jpa/0ge4 String thisContextEntity = "(" + _targetField.getJavaTypeName() /* getSourceType().getInstanceClassName() */ + ") contextEntity"; // jpa/0o05 out.println(_targetField.generateSuperSetter(varName, thisContextEntity) + ";"); } } /** * Updates the cached copy. */ @Override public void generateCopyUpdateObject(JavaWriter out, String dst, String src, int updateIndex) throws IOException { // jpa/0s29, jpa/0s2j if (getIndex() != updateIndex) return; // order matters: ejb/06gc String var = "__caucho_field_" + getName(); out.println(generateAccessor(dst, var) + " = " + generateAccessor(src, var) + ";"); /* The cache update is handled copying only the key. // ejb/0627 if (getRelatedType().getPersistenceUnit().isJPA()) { // jpa/0h0a String value = generateGet(src); value = "(" + _targetType.getInstanceClassName() + ") aConn.getEntity((com.caucho.amber.entity.Entity) " + value + ")"; out.println(generateStatementSet(dst, value) + ";"); } */ } /** * Updates the cached copy. */ @Override public void generateCopyLoadObject(JavaWriter out, String dst, String src, int updateIndex) throws IOException { if (getLoadGroupIndex() != updateIndex) return; String var = "__caucho_field_" + getName(); boolean isJPA = getEntitySourceType().getPersistenceUnit().isJPA(); // order matters: jpa/0h08, jpa/0h09 // jpa/0h20, jpa/0o08, ejb/06--, ejb/0a-- and jpa/0o04 // ejb/0628 vs. jpa/0h0a if (isJPA && ! (dst.equals("cacheEntity") || dst.equals("super") || dst.equals("item"))) { String value = generateGet(src); out.println("// " + dst); if (_targetType instanceof EntityType) { String targetTypeExt = getEntityTargetType().getInstanceClassName(); // jpa/0s2e out.println("if (isFullMerge)"); out.println(" child = " + value + ";"); out.println("else {"); out.pushDepth(); // jpa/0h0a: gets the cache object to copy from. out.println("child = aConn.getCacheEntity(" + targetTypeExt + ".class, " + var + ");"); // jpa/0o36: the cache item is only available after commit. out.println(); out.println("if (child == null && " + value + " != null)"); out.println(" child = ((com.caucho.amber.entity.Entity) " + value + ").__caucho_getCacheEntity();"); out.popDepth(); out.println("}"); } else { // XXX: jpa/0l14 out.println("child = null;"); } out.println("if (child != null) {"); out.pushDepth(); String targetTypeExt = getEntityTargetType().getInstanceClassName(); out.println("if (isFullMerge) {"); out.pushDepth(); // jpa/0j5f out.println("child = aConn.load(child.getClass(), ((com.caucho.amber.entity.Entity) child).__caucho_getPrimaryKey(), true);"); out.popDepth(); out.println("} else {"); out.pushDepth(); // jpa/0l42 out.println("com.caucho.amber.entity.Entity newChild = aConn.addNewEntity(child.getClass(), ((com.caucho.amber.entity.Entity) child).__caucho_getPrimaryKey());"); out.println("if (newChild == null) {"); out.pushDepth(); value = "aConn.getEntity((com.caucho.amber.entity.Entity) child)"; out.println("newChild = " + value + ";"); out.popDepth(); out.println("} else {"); out.pushDepth(); // jpa/0h13 out.println("((com.caucho.amber.entity.Entity) child).__caucho_copyTo(newChild, aConn, (com.caucho.amber.entity.EntityItem) null);"); out.popDepth(); out.println("}"); out.println("child = newChild;"); out.popDepth(); out.println("}"); out.popDepth(); out.println("}"); value = "(" + targetTypeExt + ") child"; // XXX: jpa/0l43 out.println("if (isFullMerge)"); out.println(" " + generateSet(dst, value) + ";"); } // jpa/0o05 // if (getLoadGroupIndex() == updateIndex) { // order matters: ejb/06gc out.println(generateAccessor(dst, var) + " = " + generateAccessor(src, var) + ";"); // jpa/0o08, jpa/0o04 if (! dst.equals("cacheEntity")) { // jpa/0o05, jpa/0h20 if (! (dst.equals("super") || dst.equals("item"))) { // || isLazy())) { String targetObject; if (isJPA) { // jpa/0h0a String targetTypeExt = getEntityTargetType().getInstanceClassName(); targetObject = "(" + targetTypeExt + ") child"; } else targetObject = generateSuperGetter("this"); String objThis = "((" + getRelatedType().getInstanceClassName() + ") " + dst + ")"; out.println(generateSuperSetter(objThis, targetObject) + ";"); } } // jpa/0o05 // } // commented out: jpa/0s29 // if (_targetLoadIndex == updateIndex) { // ejb/0h20 // } /* if (_targetLoadIndex == updateIndex) { // ejb/0a06 String value = generateGet(src); out.println(generateStatementSet(dst, value) + ";"); } */ } /** * Updates the cached copy. */ @Override public void generateMergeFrom(JavaWriter out, String dst, String src) throws IOException { if (! (getEntityTargetType() instanceof EntityType)) return; String value = generateGet(src); out.println("if (" + value + " != null) {"); out.pushDepth(); if (isCascade(CascadeType.MERGE)) { value = ("(" + getJavaTypeName() + ") aConn.recursiveMerge(" + value + ")"); } else { // jpa/0h08 value = "(" + getJavaTypeName() + ") aConn.mergeDetachedEntity((com.caucho.amber.entity.Entity) " + value + ")"; } out.println(generateSet(dst, value) + ";"); out.popDepth(); out.println("}"); } private String generateAccessor(String src, String var) { if (src.equals("super")) return var; else return "((" + getRelatedType().getInstanceClassName() + ") " + src + ")." + var; } /** * Generates the flush check for this child. */ /* @Override public boolean generateFlushCheck(JavaWriter out) throws IOException { // ejb/06bi if (! getRelatedType().getPersistenceUnit().isJPA()) return false; String getter = generateSuperGetter("this"); String dirtyVar = "__caucho_dirtyMask_" + (getIndex() / 64); long dirtyMask = (1L << (getIndex() % 64)); out.println("if ((" + getter + " != null) && (__caucho_state.isPersist() || (" + dirtyVar + " & " + dirtyMask + ") != 0L)) {"); out.pushDepth(); String relatedEntity = "((com.caucho.amber.entity.Entity) " + getter + ")"; out.println("com.caucho.amber.entity.EntityState otherState = " + relatedEntity + ".__caucho_getEntityState();"); // jpa/0j5e as a negative test. out.println("if (" + relatedEntity + ".__caucho_getConnection() == null) {"); out.pushDepth(); // jpa/0j5c as a positive test. out.println("if (__caucho_state.isTransactional() && ! otherState.isManaged())"); String errorString = ("(\"amber flush: unable to flush " + getRelatedType().getName() + "[\" + __caucho_getPrimaryKey() + \"] "+ "with non-managed dependent relationship many-to-one to "+ getEntityTargetType().getName() + ". Current entity state: \" + __caucho_state + \" " + "and parent entity state: \" + otherState)"); out.println(" throw new IllegalStateException" + errorString + ";"); out.popDepth(); out.println("}"); out.popDepth(); out.println("}"); return true; } */ /** * Generates the set clause. */ @Override public void generateStatementSet(JavaWriter out, String pstmt, String index, String source) throws IOException { if (_aliasField != null) return; if (source == null) { throw new NullPointerException(); } String var = "__caucho_field_" + getName(); if (! source.equals("this") && ! source.equals("super")) var = source + "." + var; if (! (isAbstract() && getRelatedType().getPersistenceUnit().isJPA())) { // jpa/1004, ejb/06bi out.println("if (" + var + " != null) {"); } else if (isCascade(CascadeType.PERSIST)) { // jpa/0h09 out.println("if (" + var + " != null) {"); } else { // jpa/0j58: avoids breaking FK constraints. // The "one" end in the many-to-one relationship. String amberVar = getFieldName(); out.println("com.caucho.amber.entity.EntityState " + amberVar + "_state = (" + var + " == null) ? "); out.println("com.caucho.amber.entity.EntityState.TRANSIENT : "); out.println("((com.caucho.amber.entity.Entity) " + amberVar + ")."); out.println("__caucho_getEntityState();"); out.println("if (" + amberVar + "_state.isTransactional()) {"); } out.pushDepth(); Id id = getEntityTargetType().getId(); ArrayList<IdField> keys = id.getKeys(); if (keys.size() == 1) { IdField key = keys.get(0); key.getType().generateSet(out, pstmt, index, key.getType().generateCastFromObject(var)); } else { for (int i = 0; i < keys.size(); i++) { IdField key = keys.get(i); key.getType().generateSet(out, pstmt, index, key.generateGetKeyProperty(var)); } } out.popDepth(); out.println("} else {"); out.pushDepth(); for (int i = 0; i < keys.size(); i++) { IdField key = keys.get(i); key.getType().generateSetNull(out, pstmt, index); } out.popDepth(); out.println("}"); } /** * Generates loading cache */ @Override public void generateUpdateFromObject(JavaWriter out, String obj) throws IOException { String var = "__caucho_field_" + getName(); out.println(var + " = " + obj + "." + var + ";"); } /** * Updates the cached copy. */ @Override public void generatePrePersist(JavaWriter out) throws IOException { EntityType targetType = getEntityTargetType(); if (! (targetType instanceof EntityType)) return; String fieldVar = "__caucho_field_" + getName(); String className = targetType.getInstanceClassName(); String var = "v_" + out.generateId(); out.println(); out.println(className + " " + var + " = (" + className + ") " + generateSuperGetter("this") + ";"); Id id = targetType.getId(); out.println("if (" + var + " != null) {"); out.pushDepth(); if (isCascade(CascadeType.PERSIST)) { out.println(var + " = (" + className + ") aConn.persistFromCascade(" + var + ");"); out.println(var + ".__caucho_flush();"); } out.print(fieldVar + " = "); out.print(id.toObject(id.generateGet(var))); out.println(";"); String loadVar = "__caucho_loadMask_" + (_targetLoadIndex / 64); long loadMask = (_targetLoadIndex % 64L); out.println(loadVar + " |= " + loadMask + "L;"); out.println(generateSuperSetter("this", var) + ";"); out.popDepth(); out.println("} else {"); out.println(" " + fieldVar + " = null;"); out.println("}"); } /** * Generates code for foreign entity create/delete */ @Override public void generateInvalidateForeign(JavaWriter out) throws IOException { out.println("if (\"" + _targetType.getTable().getName() + "\".equals(table)) {"); out.pushDepth(); String loadVar = "__caucho_loadMask_" + (_targetLoadIndex / 64); out.println(loadVar + " = 0L;"); out.popDepth(); out.println("}"); } /** * Generates any pre-delete code */ @Override public void generatePreDelete(JavaWriter out) throws IOException { if (! isTargetCascadeDelete()) return; String var = "caucho_field_" + getName(); out.println(getJavaTypeName() + " " + var + " = " + getGetterName() + "();"); } /** * Generates any pre-delete code */ @Override public void generatePostDelete(JavaWriter out) throws IOException { if (! isTargetCascadeDelete()) return; String var = "caucho_field_" + getName(); out.println("if (" + var + " != null) {"); out.println(" try {"); // out.println(" __caucho_session.delete(" + var + ");"); out.println(" " + var + ".remove();"); out.println(" } catch (Exception e) {"); out.println(" throw com.caucho.amber.AmberRuntimeException.create(e);"); out.println(" }"); out.println("}"); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?