Bug fix
This commit is contained in:
parent
9d85e3da6c
commit
f16eefb1f1
2 changed files with 18 additions and 14 deletions
|
|
@ -14,11 +14,9 @@ import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import zutil.db.DBConnection;
|
import zutil.db.DBConnection;
|
||||||
import zutil.log.CompactLogFormatter;
|
|
||||||
import zutil.log.LogUtil;
|
import zutil.log.LogUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -197,24 +195,24 @@ public abstract class DBBean {
|
||||||
query.append( " SET" );
|
query.append( " SET" );
|
||||||
query.append( params );
|
query.append( params );
|
||||||
if( id != null )
|
if( id != null )
|
||||||
query.append( ", id=?" );
|
query.append( "WHERE id=?" );
|
||||||
}
|
}
|
||||||
logger.fine("Save query: "+query.toString());
|
logger.finest("Save query: "+query.toString());
|
||||||
PreparedStatement stmt = db.getPreparedStatement( query.toString() );
|
PreparedStatement stmt = db.getPreparedStatement( query.toString() );
|
||||||
// Put in the variables in the SQL
|
// Put in the variables in the SQL
|
||||||
for(int i=0; i<config.fields.size() ;++i ){
|
int index = 1;
|
||||||
Field field = config.fields.get(i);
|
for(Field field : config.fields){
|
||||||
|
|
||||||
// Another DBBean class
|
// Another DBBean class
|
||||||
if( DBBean.class.isAssignableFrom( field.getType() )){
|
if( DBBean.class.isAssignableFrom( field.getType() )){
|
||||||
DBBean subobj = (DBBean)getFieldValue(field);
|
DBBean subobj = (DBBean)getFieldValue(field);
|
||||||
if(subobj != null){
|
if(subobj != null){
|
||||||
subobj.save(db);
|
subobj.save(db);
|
||||||
DBBeanConfig subconfig = getBeanConfig(subobj.getClass());
|
stmt.setObject(index, subobj.getId() );
|
||||||
stmt.setObject(i+1, subobj.getId() );
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
stmt.setObject(i+1, null);
|
stmt.setObject(index, null);
|
||||||
|
index++;
|
||||||
}
|
}
|
||||||
// A list of DBBeans
|
// A list of DBBeans
|
||||||
else if( List.class.isAssignableFrom( field.getType() ) &&
|
else if( List.class.isAssignableFrom( field.getType() ) &&
|
||||||
|
|
@ -243,11 +241,12 @@ public abstract class DBBean {
|
||||||
// Normal field
|
// Normal field
|
||||||
else{
|
else{
|
||||||
Object value = getFieldValue(field);
|
Object value = getFieldValue(field);
|
||||||
stmt.setObject(i+1, value);
|
stmt.setObject(index, value);
|
||||||
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if( id != null )
|
if( id != null )
|
||||||
stmt.setObject(config.fields.size()+1, id);
|
stmt.setObject(index, id);
|
||||||
|
|
||||||
// Execute the SQL
|
// Execute the SQL
|
||||||
DBConnection.exec(stmt);
|
DBConnection.exec(stmt);
|
||||||
|
|
@ -327,7 +326,8 @@ public abstract class DBBean {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a specific table for the given Bean
|
* Creates a specific table for the given Bean,
|
||||||
|
* WARNING: Experimental
|
||||||
*/
|
*/
|
||||||
public static void create(DBConnection sql, Class<? extends DBBean> c) throws SQLException{
|
public static void create(DBConnection sql, Class<? extends DBBean> c) throws SQLException{
|
||||||
if( !beanConfigs.containsKey( c ) )
|
if( !beanConfigs.containsKey( c ) )
|
||||||
|
|
@ -341,7 +341,7 @@ public abstract class DBBean {
|
||||||
// ID
|
// ID
|
||||||
query.append(" id ");
|
query.append(" id ");
|
||||||
query.append( classToDBName( Long.class ) );
|
query.append( classToDBName( Long.class ) );
|
||||||
query.append(" PRIMARY KEY, ");
|
query.append(" PRIMARY KEY AUTO_INCREMENT, ");
|
||||||
|
|
||||||
for( Field field : config.fields ){
|
for( Field field : config.fields ){
|
||||||
query.append(" ");
|
query.append(" ");
|
||||||
|
|
@ -375,6 +375,8 @@ public abstract class DBBean {
|
||||||
else if(c == boolean.class) return "BOOLEAN";
|
else if(c == boolean.class) return "BOOLEAN";
|
||||||
else if(c == Byte.class) return "BINARY(1)";
|
else if(c == Byte.class) return "BINARY(1)";
|
||||||
else if(c == byte.class) return "BINARY(1)";
|
else if(c == byte.class) return "BINARY(1)";
|
||||||
|
else if(DBBean.class.isAssignableFrom(c))
|
||||||
|
return classToDBName(Long.class);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,9 +28,11 @@ public class LogUtil {
|
||||||
for(int i=1; i<stackTraceElements.length ;++i){
|
for(int i=1; i<stackTraceElements.length ;++i){
|
||||||
String name = stackTraceElements[i].getClassName();
|
String name = stackTraceElements[i].getClassName();
|
||||||
//name = name.substring( name.lastIndexOf('.')+1 );
|
//name = name.substring( name.lastIndexOf('.')+1 );
|
||||||
if( !name.equals( LogUtil.class.getName() ) )
|
if( !name.equals( LogUtil.class.getName() ) ){
|
||||||
|
//System.out.println("\""+name+"\"");
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue