manytoonefield.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,403 行 · 第 1/3 页

JAVA
1,403
字号
    if (_isInsert && _aliasField == null)      _linkColumns.generateInsert(columns);  }  /**   * Generates the select clause.   */  @Override  public String generateLoadSelect(AmberTable table, String id)  {    if (_aliasField != null)      return null;    if (_linkColumns == null) {      // jpa/0ge3      return null;    }    if (_linkColumns.getSourceTable() != table)      return null;    else      return _linkColumns.generateSelectSQL(id);  }  /**   * Generates the select clause.   */  @Override  public String generateSelect(String id)  {    if (_aliasField != null)      return null;    return _linkColumns.generateSelectSQL(id);  }  /**   * Generates the update set clause   */  @Override  public void generateUpdate(CharBuffer sql)  {    if (_aliasField != null)      return;    if (_isUpdate) {      sql.append(_linkColumns.generateUpdateSQL());    }  }  /**   * Generates any prologue.   */  @Override  public void generatePrologue(JavaWriter out, HashSet<Object> completedSet)    throws IOException  {    super.generatePrologue(out, completedSet);    out.println();    Id id = getEntityTargetType().getId();    out.println("protected transient " + id.getForeignTypeName() + " __caucho_field_" + getName() + ";");    if (_aliasField == null) {      id.generatePrologue(out, completedSet, getName());    }  }  /**   * Generates the linking for a join   */  public void generateJoin(CharBuffer cb,                           String sourceTable,                           String targetTable)  {    cb.append(_linkColumns.generateJoin(sourceTable, targetTable));  }  /**   * Generates loading code   */  @Override  public int generateLoad(JavaWriter out, String rs,                          String indexVar, int index)    throws IOException  {    if (_aliasField != null)      return index;    out.print("__caucho_field_" + getName() + " = ");    index = getEntityTargetType().getId().generateLoadForeign(out, rs,                                                              indexVar, index,                                                              getName());    out.println(";");    /*    // ejb/0a06    String proxy = "aConn.loadProxy(\"" + getEntityTargetType().getName() + "\", __caucho_field_" + getName() + ")";    proxy = "(" + getEntityTargetType().getProxyClass().getName() + ") " + proxy;    out.println(generateSuperSetterMethod(proxy) + ";");    */    // commented out jpa/0l40    // out.println(generateSuperSetterMethod("null") + ";");    int group = _targetLoadIndex / 64;    long mask = (1L << (_targetLoadIndex % 64));    //out.println("__caucho_loadMask_" + group + " &= ~" + mask + "L;");    return index;  }  /* XXX: moved to generatePostLoadSelect()   * Generates loading code   *  public int generateLoadEager(JavaWriter out, String rs,                               String indexVar, int index)    throws IOException  {  }  */  /**   * Generates loading code after the basic fields.   */  @Override  public int generatePostLoadSelect(JavaWriter out, int index)    throws IOException  {    if (! isLazy()) {      out.println(getGetterName() + "();");    }    return ++index;  }  /**   * Generates the get property.   */  @Override  public void generateGetterMethod(JavaWriter out)    throws IOException  {    // jpa/0h07, jpa/0h08    // jpa/0o03, jpa/0o05, jpa/0o09    // jpa/0s2d, jpa/1810    String javaType = getJavaTypeName();    out.println();    out.println("public " + javaType + " " + getGetterName() + "()");    out.println("{");    out.pushDepth();    int keyLoadIndex = getLoadGroupIndex();    int entityLoadIndex = _targetLoadIndex;    int group = entityLoadIndex / 64;    long mask = (1L << (entityLoadIndex % 64));    String loadVar = "__caucho_loadMask_" + group;    // jpa/0h29    out.println("if (__caucho_session == null || __caucho_state.isDeleting()) {");    out.println("  return " + generateSuperGetter("this") + ";");    out.println("}");    /* XXX: jpa/0h04    if (isLazy())      out.println(" && (" + loadVar + " & " + mask + "L) == 0) {");    else {      // jpa/0o03      out.println(") {");    }    */    out.println();    String index = "_" + group;    index += "_" + mask;    if (_aliasField == null) {      // XXX: possibly bypassing of caching      out.println("if ((" + loadVar + " & " + mask + "L) == 0)");      out.println("  __caucho_load_select_" + getLoadGroupIndex() + "(__caucho_session);");    }    out.println(loadVar + " |= " + mask + "L;");    String varName = generateLoadProperty(out, index, "__caucho_session");    out.println("return " + varName + ";");    out.popDepth();    out.println("}");  }  /**   * Generates the set property.   */  public String generateLoadProperty(JavaWriter out,                                     String index,                                     String session)    throws IOException  {    boolean isJPA = getRelatedType().getPersistenceUnit().isJPA();    String targetTypeExt = _targetType.getInstanceClassName();    String otherKey;    if (_aliasField == null)      otherKey = "__caucho_field_" + getName();    else      otherKey = _aliasField.generateGet("super");    String proxyType = getEntityTargetType().getProxyClass().getName();    boolean isProxy = ! isJPA;    String varName = "v" + index;    String proxyVarName;    if (isProxy)      proxyVarName = "p" + index;    else      proxyVarName = varName;    if (isProxy)      out.println(proxyType + " " + proxyVarName + " = null;");    out.println(targetTypeExt + " " + varName + " = null;");    out.println();    Id id = getEntityTargetType().getId();    // jpa/0s2d    String nullTest = otherKey + " != null";    /* XXX    if (id instanceof CompositeId) {    }    else {      KeyPropertyField key = (KeyPropertyField) id.getKeys().get(0);      nullTest = key.getColumn().getType().generateIsNotNull(otherKey);    }    */    // jpa/0h27    out.println("if (" + nullTest + ") {");    out.pushDepth();    long targetGroup = 0;    long targetMask = 0;    // jpa/0s2e as a negative test.    if (_targetField != null) {      // jpa/0l42      long targetLoadIndex = _targetField.getTargetLoadIndex();      targetGroup = targetLoadIndex / 64;      targetMask = (1L << (targetLoadIndex % 64));    }    out.println(varName + " = (" + targetTypeExt + ") "                + session + ".loadEntity("                + targetTypeExt + ".class, "                + otherKey + ", " + ! isLazy() + ");");    /*    // jpa/0j67    out.println("if (" + varName + " != null && " + varName + " != " + generateSuperGetter("this") + ") {");    out.pushDepth();    // ejb/069a    if (isJPA && _targetField != null) {      out.println(_targetField.generateSet(varName, "this") + ";");      out.println(varName + ".__caucho_retrieve_eager(" + session + ");");    }    // generateSetTargetLoadMask(out, varName);        out.popDepth();    out.println("}");    */    // ejb/06h0, jpa/0o03    if (isAbstract() && (isLazy() || ! isJPA)) {      String proxy = session + ".loadProxy(\"" + getEntityTargetType().getName() + "\", __caucho_field_" + getName() + ")";      // jpa/0o09      if (isJPA)        proxyType = targetTypeExt;      proxy = proxyVarName + " = (" + proxyType + ") " + proxy + ";";      out.println(proxy);    }    else {      /*      // jpa/0h24      out.println("if (! " + varName + ".__caucho_getEntityState().isManaged()) {");      out.pushDepth();      long targetMask = 0;      long targetGroup = 0;      if (_targetField != null) {        long targetLoadIndex = _targetField.getTargetLoadIndex();        targetGroup = targetLoadIndex / 64;        targetMask = (1L << (targetLoadIndex % 64));      }      // jpa/0o03, jpa/0ge4      out.println(session + ".loadFromHome(" + targetTypeExt + ".class.getName(), " + otherKey + ", " + targetMask + ", " + targetGroup + ");");      out.popDepth();      out.println("}");      */    }    out.popDepth();    out.println("}");    out.println(generateSuperSetter("this", proxyVarName) + ";");    return proxyVarName;  }  /**   * Generates the set property.   */  @Override  public void generateSetterMethod(JavaWriter out)    throws IOException  {    out.println();    out.println("public void " + getSetterName() + "(" + getJavaTypeName() + " v)");    out.println("{");    out.pushDepth();    out.println("if (__caucho_session == null) {");    out.println("  " + generateSuperSetter("this", "v") + ";");    out.println("  return;");    out.println("}");    String targetClassName = getEntityTargetType().getInstanceClassName();        // ejb/06gc - updates with EJB 2.0    Id id = getEntityTargetType().getId();    String var = "__caucho_field_" + getName();    String keyType = getEntityTargetType().getId().getForeignTypeName();    int group = getLoadGroupIndex() / 64;    long loadMask = (1L << (getLoadGroupIndex() % 64));    String loadVar = "__caucho_loadMask_" + group;    if (_aliasField == null) {      out.println();      out.println("if ((" + loadVar + " & " + loadMask + "L) == 0) {");      // ejb/0602      out.println("  __caucho_load_select_" + group + "(__caucho_session);");      //out.println();      // jpa/0j5f      //out.println("  if (__caucho_session.isActiveTransaction())");      //out.println("    __caucho_session.makeTransactional((com.caucho.amber.entity.Entity) this);");      out.println("}");            out.println();      out.println(generateSuperSetter("this", "v") + ";");            out.println();      out.println("if (v == null) {");      out.println("  if (" + var + " == null)");      out.println("    return;");      out.println();      out.println("  " + var + " = null;");      out.println("} else {");      out.pushDepth();      out.println(targetClassName + " newV = (" + targetClassName + ") v;");      out.print(keyType + " key = ");      EntityType targetType = getEntityTargetType();      if (targetType.isEJBProxy(getJavaTypeName())) {        // To handle EJB local objects.        out.print(id.generateGetProxyKey("v"));      }      else {        out.print(id.toObject(id.generateGet("newV")));      }      out.println(";");      out.println();      out.println("if (key.equals(" + var + "))");      out.println("  return;");      out.println();      out.println(var + " = key;");      out.popDepth();      out.println("}");      out.println();      int entityGroup = _targetLoadIndex / 64;      long entityLoadMask = (1L << (_targetLoadIndex % 64));      String entityLoadVar = "__caucho_loadMask_" + group;      out.println(entityLoadVar + " |= " + entityLoadMask + "L;");      String dirtyVar = "__caucho_dirtyMask_" + (getIndex() / 64);      long dirtyMask = (1L << (getIndex() % 64));      // jpa/0o42: merge()      out.println(dirtyVar + " |= " + dirtyMask + "L;");      /*      out.println("try {");      out.pushDepth();      // XXX: jpa/0h27      out.println("if (__caucho_state.isPersist())");      out.println("  __caucho_cascadePrePersist(__caucho_session);");      out.popDepth();      out.println("} catch (RuntimeException e) {");      out.println("  throw e;");      out.println("} catch (Exception e) {");      out.println("  throw new com.caucho.amber.AmberRuntimeException(e);");      out.println("}");      */      out.println("__caucho_session.update((com.caucho.amber.entity.Entity) this);");      out.println("__caucho_session.addCompletion(__caucho_home.createManyToOneCompletion(\"" + getName() + "\", (com.caucho.amber.entity.Entity) this, v));");      out.println();    }    else {      out.println("throw new IllegalStateException(\"aliased field cannot be set\");");    }    out.popDepth();    out.println("}");  }  void generateSetTargetLoadMask(JavaWriter out, String varName)    throws IOException  {    // jpa/0o0-, jpa/0ge4    boolean isJPA = getRelatedType().getPersistenceUnit().isJPA();    if (_targetField != null && isJPA) {      long targetLoadIndex = _targetField.getTargetLoadIndex();      long targetGroup = targetLoadIndex / 64;      long targetMask = (1L << (targetLoadIndex % 64));

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?