Fixed DBBean TC

This commit is contained in:
Ziver Koc 2017-02-13 17:58:32 +01:00
parent 1aedfb52b7
commit 4f26a1ae87
2 changed files with 13 additions and 13 deletions

View file

@ -238,12 +238,12 @@ public abstract class DBBean {
List<DBBean> list = (List<DBBean>)getFieldValue(field); List<DBBean> list = (List<DBBean>)getFieldValue(field);
if( list != null ){ if( list != null ){
DBLinkTable linkTable = field.getAnnotation( DBLinkTable.class ); DBLinkTable linkTableAnnotation = field.getAnnotation( DBLinkTable.class );
String subTable = linkTable.table(); String linkTable = linkTableAnnotation.table();
String idCol = (linkTable.idColumn().isEmpty() ? config.tableName : linkTable.idColumn() ); String idCol = (linkTableAnnotation.idColumn().isEmpty() ? config.tableName : linkTableAnnotation.idColumn() );
String subIdCol = "id"; String subIdCol = "id";
DBBeanConfig subConfig = null; DBBeanConfig subObjConfig = null;
for(DBBean subObj : list){ for(DBBean subObj : list){
// Save the sub bean // Save the sub bean
if( recursive || subObj.getId() == null ) if( recursive || subObj.getId() == null )
@ -253,17 +253,17 @@ public abstract class DBBean {
continue; continue;
} }
// Get the Sub object configuration // Get the Sub object configuration
if(subConfig == null){ if(subObjConfig == null){
subConfig = DBBeanConfig.getBeanConfig( subObj.getClass() ); subObjConfig = DBBeanConfig.getBeanConfig( subObj.getClass() );
subIdCol = subConfig.idColumn; subIdCol = subObjConfig.idColumn;
} }
// Save links in link table // Save links in link table
String sql; String sql;
if( subTable.equals(subConfig.tableName) ) if(linkTable.equals(subObjConfig.tableName))
sql = "UPDATE "+subTable+" SET "+idCol+"=? WHERE "+subIdCol+"=?"; sql = "UPDATE "+linkTable+" SET "+idCol+"=? WHERE "+subIdCol+"=?";
else else // TODO: REPLACE will probably not work here
sql = "REPLACE INTO "+subTable+" SET "+idCol+"=?, "+subIdCol+"=?"; sql = "REPLACE INTO "+linkTable+" ("+idCol+", "+subIdCol+") VALUES(?, ?)";
logger.finest("Save Bean("+c.getName()+", id: "+subObj.getId()+") query: "+sql); logger.finest("Save sub Bean("+c.getName()+", id: "+subObj.getId()+") query: "+sql);
PreparedStatement subStmt = db.getPreparedStatement( sql ); PreparedStatement subStmt = db.getPreparedStatement( sql );
subStmt.setLong(1, this.id ); subStmt.setLong(1, this.id );
subStmt.setLong(2, subObj.getId() ); subStmt.setLong(2, subObj.getId() );

View file

@ -180,7 +180,7 @@ public class DBBeanSaveTest {
"parent_id INTEGER, " + "parent_id INTEGER, " +
"intField INTEGER);"); "intField INTEGER);");
ParentTestClass obj = new ParentTestClass(); ParentLinkTestClass obj = new ParentLinkTestClass();
SubObjectTestClass subObj = new SubObjectTestClass(); SubObjectTestClass subObj = new SubObjectTestClass();
subObj.intField = 1337; subObj.intField = 1337;
obj.subobjs.add(subObj); obj.subobjs.add(subObj);