Bug Fix :D
This commit is contained in:
parent
b75174c3fc
commit
9f3c8a886e
2 changed files with 14 additions and 14 deletions
|
|
@ -63,12 +63,12 @@ public abstract class DBBean {
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target(ElementType.FIELD)
|
@Target(ElementType.FIELD)
|
||||||
public @interface DBLinkTable {
|
public @interface DBLinkTable {
|
||||||
/** The name of the Link table */
|
/** The name of the Link table, should not contain any strange characters or spaces */
|
||||||
String name();
|
String name();
|
||||||
/** The class of the linked bean */
|
/** The class of the linked bean */
|
||||||
Class<? extends DBBean> beanClass();
|
Class<? extends DBBean> beanClass();
|
||||||
/** The name of the column that contains this objects id */
|
/** The name of the column that contains the main objects id */
|
||||||
String column() default "";
|
String idColumn() default "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -231,7 +231,7 @@ public abstract class DBBean {
|
||||||
if( list != null ){
|
if( list != null ){
|
||||||
DBLinkTable linkTable = field.getAnnotation( DBLinkTable.class );
|
DBLinkTable linkTable = field.getAnnotation( DBLinkTable.class );
|
||||||
String subtable = linkTable.name();
|
String subtable = linkTable.name();
|
||||||
String idcol = (linkTable.column().isEmpty() ? config.tableName : linkTable.column() );
|
String idcol = (linkTable.idColumn().isEmpty() ? config.tableName : linkTable.idColumn() );
|
||||||
String sub_idcol = "id";
|
String sub_idcol = "id";
|
||||||
|
|
||||||
DBBeanConfig subConfig = null;
|
DBBeanConfig subConfig = null;
|
||||||
|
|
|
||||||
|
|
@ -135,7 +135,7 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
obj = bean_class.newInstance();
|
obj = bean_class.newInstance();
|
||||||
cacheDBBean(obj, id);
|
cacheDBBean(obj, id);
|
||||||
|
|
||||||
// Get id field
|
// Set id field
|
||||||
obj.id = id;
|
obj.id = id;
|
||||||
// Get the rest
|
// Get the rest
|
||||||
for( Field field : bean_config.fields ){
|
for( Field field : bean_config.fields ){
|
||||||
|
|
@ -144,7 +144,7 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
// Another DBBean class
|
// Another DBBean class
|
||||||
if( DBBean.class.isAssignableFrom( field.getType() )){
|
if( DBBean.class.isAssignableFrom( field.getType() )){
|
||||||
if(db != null){
|
if(db != null){
|
||||||
Object subid = result.getObject(name);
|
Long subid = result.getLong( name );
|
||||||
DBBean subobj = getCachedDBBean(field.getType(), subid);
|
DBBean subobj = getCachedDBBean(field.getType(), subid);
|
||||||
if( subobj == null )
|
if( subobj == null )
|
||||||
subobj = DBBean.load(db, (Class<? extends DBBean>)field.getType(), subid);
|
subobj = DBBean.load(db, (Class<? extends DBBean>)field.getType(), subid);
|
||||||
|
|
@ -156,15 +156,17 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
field.getAnnotation( DBLinkTable.class ) != null){
|
field.getAnnotation( DBLinkTable.class ) != null){
|
||||||
if(db != null){
|
if(db != null){
|
||||||
DBLinkTable linkTable = field.getAnnotation( DBLinkTable.class );
|
DBLinkTable linkTable = field.getAnnotation( DBLinkTable.class );
|
||||||
String subtable = linkTable.name();
|
DBBeanConfig subConfig = DBBean.getBeanConfig( linkTable.beanClass() );
|
||||||
String idcol = (linkTable.column().isEmpty() ? bean_config.tableName : linkTable.column() );
|
String linkTableName = linkTable.name();
|
||||||
|
String subTable = subConfig.tableName;
|
||||||
|
String idcol = (linkTable.idColumn().isEmpty() ? bean_config.tableName : linkTable.idColumn() );
|
||||||
|
|
||||||
// Load list from link table
|
// Load list from link table
|
||||||
String subsql = "SELECT * FROM "+subtable+" WHERE ?=?";
|
//String subsql = "SELECT * FROM "+linkTableName+" NATURAL JOIN "+subConfig.tableName+" WHERE "+idcol+"=?";
|
||||||
|
String subsql = "SELECT obj.* FROM "+linkTableName+" as link, "+subTable+" as obj WHERE obj."+idcol+"=? AND obj.id=link.id";
|
||||||
logger.finest("List Load Query: "+subsql);
|
logger.finest("List Load Query: "+subsql);
|
||||||
PreparedStatement subStmt = db.getPreparedStatement( subsql );
|
PreparedStatement subStmt = db.getPreparedStatement( subsql );
|
||||||
subStmt.setString(1, idcol);
|
subStmt.setObject(1, obj.getId() );
|
||||||
subStmt.setObject(2, obj.getId() );
|
|
||||||
List<? extends DBBean> list = DBConnection.exec(subStmt,
|
List<? extends DBBean> list = DBConnection.exec(subStmt,
|
||||||
DBBeanSQLResultHandler.createList(linkTable.beanClass(), db));
|
DBBeanSQLResultHandler.createList(linkTable.beanClass(), db));
|
||||||
obj.setFieldValue(field, list);
|
obj.setFieldValue(field, list);
|
||||||
|
|
@ -176,9 +178,7 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
|
|
||||||
} catch (InstantiationException e) {
|
} catch (Exception e) {
|
||||||
throw new SQLException(e);
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
throw new SQLException(e);
|
throw new SQLException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue