Bug fix
This commit is contained in:
parent
efae412a88
commit
64cf6b09bc
3 changed files with 60 additions and 27 deletions
|
|
@ -10,6 +10,7 @@ import java.math.BigDecimal;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Timestamp;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -35,7 +36,7 @@ import zutil.log.LogUtil;
|
||||||
* *String
|
* *String
|
||||||
* *Character
|
* *Character
|
||||||
* *DBBean
|
* *DBBean
|
||||||
* *java.sql.Date
|
* *java.sql.Timestamp
|
||||||
* *List<DBBean>
|
* *List<DBBean>
|
||||||
*
|
*
|
||||||
* </XMP>
|
* </XMP>
|
||||||
|
|
@ -138,11 +139,23 @@ public abstract class DBBean {
|
||||||
beanConfigs.put(c, config);
|
beanConfigs.put(c, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the object and all the sub objects to the DB
|
||||||
|
*
|
||||||
|
* @param db is the DBMS connection
|
||||||
|
*/
|
||||||
|
public void save(DBConnection db) throws SQLException{
|
||||||
|
save( db, true );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves the Object to the DB
|
* Saves the Object to the DB
|
||||||
|
*
|
||||||
|
* @param db is the DBMS connection
|
||||||
|
* @param recursive is if the method should save all sub objects
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void save(DBConnection db) throws SQLException{
|
public void save(DBConnection db, boolean recursive) throws SQLException{
|
||||||
if( processing )
|
if( processing )
|
||||||
return;
|
return;
|
||||||
processing = true;
|
processing = true;
|
||||||
|
|
@ -182,7 +195,8 @@ public abstract class DBBean {
|
||||||
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);
|
if( recursive || subobj.getId() == null )
|
||||||
|
subobj.save(db);
|
||||||
stmt.setObject(index, subobj.getId() );
|
stmt.setObject(index, subobj.getId() );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -192,26 +206,7 @@ public abstract class DBBean {
|
||||||
// A list of DBBeans
|
// A list of DBBeans
|
||||||
else if( List.class.isAssignableFrom( field.getType() ) &&
|
else if( List.class.isAssignableFrom( field.getType() ) &&
|
||||||
field.getAnnotation( DBLinkTable.class ) != null){
|
field.getAnnotation( DBLinkTable.class ) != null){
|
||||||
List<DBBean> list = (List<DBBean>)getFieldValue(field);
|
// DO NOTING
|
||||||
if( list != null ){
|
|
||||||
DBLinkTable linkTable = field.getAnnotation( DBLinkTable.class );
|
|
||||||
String subtable = linkTable.name();
|
|
||||||
String idcol = (linkTable.column().isEmpty() ? config.tableName : linkTable.column() );
|
|
||||||
|
|
||||||
DBBeanConfig subConfig = null;
|
|
||||||
for(DBBean subobj : list){
|
|
||||||
if(subConfig == null)
|
|
||||||
subConfig = beanConfigs.get( subobj.getClass() );
|
|
||||||
// Save links in link table
|
|
||||||
PreparedStatement subStmt = db.getPreparedStatement("REPLACE INTO "+subtable+" SET ?=? id=?");
|
|
||||||
subStmt.setString(1, idcol);
|
|
||||||
subStmt.setString(2, subConfig.tableName);
|
|
||||||
subStmt.setObject(3, subobj.getId() );
|
|
||||||
DBConnection.exec(subStmt);
|
|
||||||
// Save the sub bean
|
|
||||||
subobj.save(db);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Normal field
|
// Normal field
|
||||||
else{
|
else{
|
||||||
|
|
@ -227,6 +222,37 @@ public abstract class DBBean {
|
||||||
DBConnection.exec(stmt);
|
DBConnection.exec(stmt);
|
||||||
if( id == null )
|
if( id == null )
|
||||||
this.id = (Long) db.getLastInsertID();
|
this.id = (Long) db.getLastInsertID();
|
||||||
|
|
||||||
|
// Save the list, after we get the object id
|
||||||
|
for(Field field : config.fields){
|
||||||
|
if( List.class.isAssignableFrom( field.getType() ) &&
|
||||||
|
field.getAnnotation( DBLinkTable.class ) != null){
|
||||||
|
List<DBBean> list = (List<DBBean>)getFieldValue(field);
|
||||||
|
if( list != null ){
|
||||||
|
DBLinkTable linkTable = field.getAnnotation( DBLinkTable.class );
|
||||||
|
String subtable = linkTable.name();
|
||||||
|
String idcol = (linkTable.column().isEmpty() ? config.tableName : linkTable.column() );
|
||||||
|
String sub_idcol = "id";
|
||||||
|
|
||||||
|
DBBeanConfig subConfig = null;
|
||||||
|
for(DBBean subobj : list){
|
||||||
|
// Save the sub bean
|
||||||
|
if( recursive || subobj.getId() == null )
|
||||||
|
subobj.save(db);
|
||||||
|
// Get the Sub object configuration
|
||||||
|
if(subConfig == null)
|
||||||
|
subConfig = beanConfigs.get( subobj.getClass() );
|
||||||
|
// Save links in link table
|
||||||
|
String subsql = "REPLACE INTO "+subtable+" SET "+idcol+"=?, "+sub_idcol+"=?";
|
||||||
|
logger.finest("List Save query: "+subsql);
|
||||||
|
PreparedStatement subStmt = db.getPreparedStatement( subsql );
|
||||||
|
subStmt.setLong(1, this.getId() );
|
||||||
|
subStmt.setLong(2, subobj.getId() );
|
||||||
|
DBConnection.exec(subStmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw e;
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
@ -349,6 +375,7 @@ 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(c == Timestamp.class) return "DATETIME";
|
||||||
else if(DBBean.class.isAssignableFrom(c))
|
else if(DBBean.class.isAssignableFrom(c))
|
||||||
return classToDBName(Long.class);
|
return classToDBName(Long.class);
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -393,7 +420,7 @@ public abstract class DBBean {
|
||||||
else
|
else
|
||||||
field.set(this, o);
|
field.set(this, o);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.log(Level.WARNING, e.getMessage(), e);
|
logger.log(Level.SEVERE, e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ import zutil.log.LogUtil;
|
||||||
public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
public static final Logger logger = LogUtil.getLogger();
|
public static final Logger logger = LogUtil.getLogger();
|
||||||
/** This is the time to live for the cached items **/
|
/** This is the time to live for the cached items **/
|
||||||
public static final long CACHE_TTL = 1000*60*10; // in ms
|
public static final long CACHE_TTL = 1000*60*10; // 10 min in ms
|
||||||
/** A cache for detecting recursion **/
|
/** A cache for detecting recursion **/
|
||||||
protected static HashMap<Class<?>, HashMap<Object,DBBeanCache>> cache =
|
protected static HashMap<Class<?>, HashMap<Object,DBBeanCache>> cache =
|
||||||
new HashMap<Class<?>, HashMap<Object,DBBeanCache>>();
|
new HashMap<Class<?>, HashMap<Object,DBBeanCache>>();
|
||||||
|
|
@ -102,6 +102,7 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
public T handleQueryResult(Statement stmt, ResultSet result) throws SQLException{
|
public T handleQueryResult(Statement stmt, ResultSet result) throws SQLException{
|
||||||
if( list ){
|
if( list ){
|
||||||
LinkedList<DBBean> bean_list = new LinkedList<DBBean>();
|
LinkedList<DBBean> bean_list = new LinkedList<DBBean>();
|
||||||
|
logger.fine("Loading new DBBean List");
|
||||||
while( result.next() ){
|
while( result.next() ){
|
||||||
DBBean obj = createBean(result);
|
DBBean obj = createBean(result);
|
||||||
bean_list.add( obj );
|
bean_list.add( obj );
|
||||||
|
|
@ -109,8 +110,9 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
return (T) bean_list;
|
return (T) bean_list;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
if( result.next() )
|
if( result.next() ){
|
||||||
return (T) createBean(result);
|
return (T) createBean(result);
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -129,6 +131,7 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
DBBean obj = getCachedDBBean(bean_class, id);
|
DBBean obj = getCachedDBBean(bean_class, id);
|
||||||
if( obj != null )
|
if( obj != null )
|
||||||
return obj;
|
return obj;
|
||||||
|
logger.fine("Loading new DBBean("+bean_class.getName()+") with id: "+id);
|
||||||
obj = bean_class.newInstance();
|
obj = bean_class.newInstance();
|
||||||
cacheDBBean(obj, id);
|
cacheDBBean(obj, id);
|
||||||
|
|
||||||
|
|
@ -157,7 +160,9 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
String idcol = (linkTable.column().isEmpty() ? bean_config.tableName : linkTable.column() );
|
String idcol = (linkTable.column().isEmpty() ? bean_config.tableName : linkTable.column() );
|
||||||
|
|
||||||
// Load list from link table
|
// Load list from link table
|
||||||
PreparedStatement subStmt = db.getPreparedStatement("SELECT * FROM "+subtable+" WHERE ?=?");
|
String subsql = "SELECT * FROM "+subtable+" WHERE ?=?";
|
||||||
|
logger.finest("List Load Query: "+subsql);
|
||||||
|
PreparedStatement subStmt = db.getPreparedStatement( subsql );
|
||||||
subStmt.setString(1, idcol);
|
subStmt.setString(1, idcol);
|
||||||
subStmt.setObject(2, obj.getId() );
|
subStmt.setObject(2, obj.getId() );
|
||||||
List<? extends DBBean> list = DBConnection.exec(subStmt,
|
List<? extends DBBean> list = DBConnection.exec(subStmt,
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,7 @@ public class SOAPClientFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate the methods
|
// Generate the methods
|
||||||
|
// TODO:
|
||||||
for(WSMethodDef methodDef : wsDef.getMethods()){
|
for(WSMethodDef methodDef : wsDef.getMethods()){
|
||||||
CtMethod method = CtNewMethod.make("public int m(int i){}", cc);
|
CtMethod method = CtNewMethod.make("public int m(int i){}", cc);
|
||||||
method.insertBefore("System.out.println(\"Hello.say():\");");
|
method.insertBefore("System.out.println(\"Hello.say():\");");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue