for (int i = 0; i <= loadCount / 64; i++) {
out.println("__caucho_loadMask_" + i + " = 0L;");
// XXX: jpa/0l21
EntityType parentType = _entityType;
do {
out.println("__caucho_loadMask_" + i + " |= " + parentType.getCreateLoadMask(i) + ";");
} while ((parentType = parentType.getParentType()) != null);
}
out.println();
out.println("__caucho_session = aConn;");
out.println("__caucho_home = home;");
_entityType.generatePrePersist(out);
//out.println();
//out.println("__caucho_home.prePersist(this);");
out.println();
// jpa/0r20
for (Method method : _entityType.getPrePersistCallbacks()) {
out.println(method.getName() + "();");
}
if (isGeneratedValue) {
// jpa/0g50: generated id needs to flush the insert statement at persist() time.
out.println("__caucho_create(aConn, home);");
}
else {
// jpa/0j5e: persist() is lazy but should cascade to add entities to the context.
//out.println();
//out.println("__caucho_cascadePrePersist(aConn);");
out.println("__caucho_cascadePostPersist(aConn);");
}
out.println("__caucho_home.postPersist(this);");
for (Method method : _entityType.getPostPersistCallbacks()) {
out.println(method.getName() + "();");
}
out.println();
out.println("return true;");
out.popDepth();
out.println("}");
}
out.println();
out.println("public boolean __caucho_create(com.caucho.amber.manager.AmberConnection aConn, com.caucho.amber.type.EntityType home)");
out.println(" throws java.sql.SQLException");
out.println("{");
out.pushDepth();
// jpa/0ge2: MappedSuperclassType
if ((_entityType.getTable() == null) || (_entityType.getId() == null)) {
out.println("return false;");
out.popDepth();
out.println("}");
return;
}
out.println("if (__caucho_state != com.caucho.amber.entity.EntityState.P_PERSISTING)");
out.println(" return false;");
out.println();
out.println("__caucho_state = com.caucho.amber.entity.EntityState.P_PERSISTED;");
out.println();
out.println("__caucho_cascadePrePersist(aConn);");
int dirtyCount = _entityType.getDirtyIndex();
for (int i = 0; i <= dirtyCount / 64; i++) {
out.println("__caucho_dirtyMask_" + i + " = 0L;");
}
AmberTable table = _entityType.getTable();
String sql = null;
out.println("String sql;");
boolean isAutoInsert = false;
if (_entityType.getId() != null
&& ! isAbstract
&& _entityType.getId().isIdentityGenerator()) {
isAutoInsert = true;
}
out.println("int index = 1;");
_entityType.getId().generateCheckCreateKey(out);
out.println("java.sql.PreparedStatement pstmt;");
// jpa/0gg0, jpa/0gh0
if (isAutoInsert) {
out.println("if (__caucho_home.isIdentityGenerator()) {");
out.pushDepth();
out.print("sql = \"");
out.printJavaString(_entityType.generateAutoCreateSQL(table));
out.println("\";");
out.println("pstmt = aConn.prepareInsertStatement(sql, true);");
out.popDepth();
out.println("} else {");
out.pushDepth();
}
out.print("sql = \"");
out.printJavaString(_entityType.generateCreateSQL(table));
out.println("\";");
out.println("pstmt = aConn.prepareInsertStatement(sql, false);");
if (isAutoInsert) {
out.popDepth();
out.println("}");
}
_entityType.getId().generateSetInsert(out, "pstmt", "index");
_entityType.generateInsertSet(out, table, "pstmt", "index", "super");
out.println();
out.println("pstmt.executeUpdate();");
out.println();
_entityType.getId().generateSetGeneratedKeys(out, "pstmt");
EntityType parentType = _entityType;
do {
for (AmberTable subTable : parentType.getSecondaryTables()) {
sql = parentType.generateCreateSQL(subTable);
out.println();
out.print("sql = \"");
out.printJavaString(sql);
out.println("\";");
out.println("pstmt = aConn.prepareStatement(sql);");
out.println("index = 1;");
out.println();
parentType.getId().generateSetInsert(out, "pstmt", "index");
parentType.generateInsertSet(out, subTable, "pstmt", "index", "super");
out.println();
out.println("pstmt.executeUpdate();");
out.println();
parentType.getId().generateSetGeneratedKeys(out, "pstmt");
}
} while ((parentType = parentType.getParentType()) != null);
// println("pstmt.close();");
out.println("__caucho_cacheItem = new com.caucho.amber.entity.CacheableEntityItem(home.getHome(), new " + getClassName() + "());");
out.println(getClassName() + " cacheEntity = (" + getClassName() + ") __caucho_cacheItem.getEntity();");
out.println("cacheEntity.__caucho_home = home;");
Id id = _entityType.getId();
out.println("Object pk = null;");
if (! id.isEmbeddedId()) {
ArrayList<IdField> keys = id.getKeys();
for (IdField key : keys) {
String value;
if (keys.size() == 1)
value = key.getType().generateCastFromObject("(pk = __caucho_getPrimaryKey())");
else
value = key.generateGet("super");
out.println(key.generateSet("cacheEntity", value) + ";");
}
}
else {
// jpa/0gh0
id.generateCopy(out, "cacheEntity", "this");
// out.println("pk = __caucho_compound_key;");
// out.println(id.getEmbeddedIdField().generateStatementSet("cacheEntity", "__caucho_compound_key") + ";");
}
out.println("try {");
out.pushDepth();
// jpa/0o01
out.println("Object child;");
// jpa/0l21
for (int i = 0; i <= loadCount; i++) {
_entityType.generateCopyLoadObject(out, "cacheEntity", "super", i);
}
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("}");
parentType = _entityType;
// jpa/0l21
for (int i = 0; i <= loadCount / 64; i++) {
out.println("cacheEntity.__caucho_loadMask_" + i + " = 0L;");
do {
out.println("cacheEntity.__caucho_loadMask_" + i + " |= " + parentType.getCreateLoadMask(i) + ";");
}
while ((parentType = parentType.getParentType()) != null);
}
out.println();
out.println("if (pk == null)");
out.println(" pk = __caucho_getPrimaryKey();");