Removed dynamic id field
This commit is contained in:
parent
4973247b9b
commit
9d85e3da6c
2 changed files with 28 additions and 29 deletions
|
|
@ -45,7 +45,7 @@ public abstract class DBBean {
|
||||||
public static final Logger logger = LogUtil.getLogger();
|
public static final Logger logger = LogUtil.getLogger();
|
||||||
|
|
||||||
/** The id of the bean **/
|
/** The id of the bean **/
|
||||||
private Long id;
|
protected Long id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the name of the table in the database
|
* Sets the name of the table in the database
|
||||||
|
|
@ -85,7 +85,7 @@ public abstract class DBBean {
|
||||||
/** The name of the table in the DB */
|
/** The name of the table in the DB */
|
||||||
protected String tableName;
|
protected String tableName;
|
||||||
/** The id field */
|
/** The id field */
|
||||||
protected Field id_field;
|
//protected Field id_field;
|
||||||
/** All the fields in the bean */
|
/** All the fields in the bean */
|
||||||
protected ArrayList<Field> fields;
|
protected ArrayList<Field> fields;
|
||||||
|
|
||||||
|
|
@ -108,11 +108,11 @@ public abstract class DBBean {
|
||||||
/**
|
/**
|
||||||
* @return the ID field of the bean or null if there is non
|
* @return the ID field of the bean or null if there is non
|
||||||
*/
|
*/
|
||||||
public static Field getIDField(Class<? extends DBBean> c){
|
/*public static Field getIDField(Class<? extends DBBean> c){
|
||||||
if( !beanConfigs.containsKey( c ) )
|
if( !beanConfigs.containsKey( c ) )
|
||||||
initBeanConfig( c );
|
initBeanConfig( c );
|
||||||
return beanConfigs.get( c ).id_field;
|
return beanConfigs.get( c ).id_field;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return all the fields except the ID field
|
* @return all the fields except the ID field
|
||||||
|
|
@ -155,12 +155,12 @@ public abstract class DBBean {
|
||||||
config.fields.add( field );
|
config.fields.add( field );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
/*try {
|
||||||
config.id_field = DBBean.class.getDeclaredField("id");
|
config.id_field = DBBean.class.getDeclaredField("id");
|
||||||
config.id_field.setAccessible(true);
|
config.id_field.setAccessible(true);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}*/
|
||||||
|
|
||||||
beanConfigs.put(c, config);
|
beanConfigs.put(c, config);
|
||||||
}
|
}
|
||||||
|
|
@ -176,7 +176,7 @@ public abstract class DBBean {
|
||||||
Class<? extends DBBean> c = this.getClass();
|
Class<? extends DBBean> c = this.getClass();
|
||||||
DBBeanConfig config = beanConfigs.get(c);
|
DBBeanConfig config = beanConfigs.get(c);
|
||||||
try {
|
try {
|
||||||
Object id = getFieldValue( config.id_field );
|
Long id = this.getId();
|
||||||
// Generate the SQL
|
// Generate the SQL
|
||||||
StringBuilder query = new StringBuilder();
|
StringBuilder query = new StringBuilder();
|
||||||
if( id == null )
|
if( id == null )
|
||||||
|
|
@ -197,7 +197,7 @@ public abstract class DBBean {
|
||||||
query.append( " SET" );
|
query.append( " SET" );
|
||||||
query.append( params );
|
query.append( params );
|
||||||
if( id != null )
|
if( id != null )
|
||||||
query.append( ", "+config.id_field.getName()+"=?" );
|
query.append( ", id=?" );
|
||||||
}
|
}
|
||||||
logger.fine("Save query: "+query.toString());
|
logger.fine("Save query: "+query.toString());
|
||||||
PreparedStatement stmt = db.getPreparedStatement( query.toString() );
|
PreparedStatement stmt = db.getPreparedStatement( query.toString() );
|
||||||
|
|
@ -211,7 +211,7 @@ public abstract class DBBean {
|
||||||
if(subobj != null){
|
if(subobj != null){
|
||||||
subobj.save(db);
|
subobj.save(db);
|
||||||
DBBeanConfig subconfig = getBeanConfig(subobj.getClass());
|
DBBeanConfig subconfig = getBeanConfig(subobj.getClass());
|
||||||
stmt.setObject(i+1, subobj.getFieldValue(subconfig.id_field) );
|
stmt.setObject(i+1, subobj.getId() );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
stmt.setObject(i+1, null);
|
stmt.setObject(i+1, null);
|
||||||
|
|
@ -230,11 +230,10 @@ public abstract class DBBean {
|
||||||
if(subConfig == null)
|
if(subConfig == null)
|
||||||
subConfig = beanConfigs.get( subobj.getClass() );
|
subConfig = beanConfigs.get( subobj.getClass() );
|
||||||
// Save links in link table
|
// Save links in link table
|
||||||
PreparedStatement subStmt = db.getPreparedStatement("REPLACE INTO "+subtable+" SET ?=? ?=?");
|
PreparedStatement subStmt = db.getPreparedStatement("REPLACE INTO "+subtable+" SET ?=? id=?");
|
||||||
subStmt.setString(1, idcol);
|
subStmt.setString(1, idcol);
|
||||||
subStmt.setObject(2, config.id_field);
|
subStmt.setString(2, subConfig.tableName);
|
||||||
subStmt.setString(3, subConfig.tableName);
|
subStmt.setObject(3, subobj.getId() );
|
||||||
subStmt.setObject(4, subobj.getFieldValue(subConfig.id_field) );
|
|
||||||
DBConnection.exec(subStmt);
|
DBConnection.exec(subStmt);
|
||||||
// Save the sub bean
|
// Save the sub bean
|
||||||
subobj.save(db);
|
subobj.save(db);
|
||||||
|
|
@ -253,12 +252,13 @@ public abstract class DBBean {
|
||||||
// Execute the SQL
|
// Execute the SQL
|
||||||
DBConnection.exec(stmt);
|
DBConnection.exec(stmt);
|
||||||
if( id == null )
|
if( id == null )
|
||||||
setFieldValue( config.id_field, db.getLastInsertID() );
|
this.id = (Long) db.getLastInsertID();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw e;
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new SQLException(e);
|
throw new SQLException(e);
|
||||||
}
|
}
|
||||||
|
processing = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -267,15 +267,15 @@ public abstract class DBBean {
|
||||||
public void delete(DBConnection db){
|
public void delete(DBConnection db){
|
||||||
Class<? extends DBBean> c = this.getClass();
|
Class<? extends DBBean> c = this.getClass();
|
||||||
DBBeanConfig config = beanConfigs.get(c);
|
DBBeanConfig config = beanConfigs.get(c);
|
||||||
if( config.id_field == null )
|
if( this.getId() == null )
|
||||||
throw new NoSuchElementException("ID field is null!");
|
throw new NoSuchElementException("ID field is null!");
|
||||||
try {
|
try {
|
||||||
String sql = "DELETE FROM "+config.tableName+" WHERE "+ config.id_field +"=?";
|
String sql = "DELETE FROM "+config.tableName+" WHERE id=?";
|
||||||
logger.fine("Load query: "+sql);
|
logger.fine("Load query: "+sql);
|
||||||
PreparedStatement stmt = db.getPreparedStatement( sql );
|
PreparedStatement stmt = db.getPreparedStatement( sql );
|
||||||
// Put in the variables in the SQL
|
// Put in the variables in the SQL
|
||||||
logger.fine("Delete query: "+sql);
|
logger.fine("Delete query: "+sql);
|
||||||
stmt.setObject(1, getFieldValue(config.id_field) );
|
stmt.setObject(1, this.getId() );
|
||||||
|
|
||||||
// Execute the SQL
|
// Execute the SQL
|
||||||
DBConnection.exec(stmt);
|
DBConnection.exec(stmt);
|
||||||
|
|
@ -319,9 +319,8 @@ public abstract class DBBean {
|
||||||
initBeanConfig( c );
|
initBeanConfig( c );
|
||||||
DBBeanConfig config = beanConfigs.get(c);
|
DBBeanConfig config = beanConfigs.get(c);
|
||||||
// Generate query
|
// Generate query
|
||||||
PreparedStatement stmt = db.getPreparedStatement( "SELECT * FROM "+config.tableName+" WHERE ?=? LIMIT 1" );
|
PreparedStatement stmt = db.getPreparedStatement( "SELECT * FROM "+config.tableName+" WHERE id=? LIMIT 1" );
|
||||||
stmt.setString(1, config.id_field.getName());
|
stmt.setObject(1, id );
|
||||||
stmt.setObject(2, id );
|
|
||||||
// Run query
|
// Run query
|
||||||
T obj = DBConnection.exec(stmt, DBBeanSQLResultHandler.create(c, db) );
|
T obj = DBConnection.exec(stmt, DBBeanSQLResultHandler.create(c, db) );
|
||||||
return obj;
|
return obj;
|
||||||
|
|
@ -339,12 +338,15 @@ public abstract class DBBean {
|
||||||
StringBuilder query = new StringBuilder();
|
StringBuilder query = new StringBuilder();
|
||||||
query.append("CREATE TABLE "+config.tableName+" ( ");
|
query.append("CREATE TABLE "+config.tableName+" ( ");
|
||||||
|
|
||||||
|
// ID
|
||||||
|
query.append(" id ");
|
||||||
|
query.append( classToDBName( Long.class ) );
|
||||||
|
query.append(" PRIMARY KEY, ");
|
||||||
|
|
||||||
for( Field field : config.fields ){
|
for( Field field : config.fields ){
|
||||||
query.append(" ");
|
query.append(" ");
|
||||||
query.append( field.getName() );
|
query.append( field.getName() );
|
||||||
query.append( classToDBName(c) );
|
query.append( classToDBName(c) );
|
||||||
if( config.id_field.equals( field ) )
|
|
||||||
query.append(" PRIMARY KEY");
|
|
||||||
query.append(", ");
|
query.append(", ");
|
||||||
}
|
}
|
||||||
query.delete( query.length()-2, query.length());
|
query.delete( query.length()-2, query.length());
|
||||||
|
|
|
||||||
|
|
@ -125,7 +125,7 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private DBBean createBean(ResultSet result) throws SQLException{
|
private DBBean createBean(ResultSet result) throws SQLException{
|
||||||
try {
|
try {
|
||||||
Object id = result.getObject( bean_config.id_field.getName() );
|
Long id = result.getLong( "id" );
|
||||||
DBBean obj = getCachedDBBean(bean_class, id);
|
DBBean obj = getCachedDBBean(bean_class, id);
|
||||||
if( obj != null )
|
if( obj != null )
|
||||||
return obj;
|
return obj;
|
||||||
|
|
@ -133,12 +133,9 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
cacheDBBean(obj, id);
|
cacheDBBean(obj, id);
|
||||||
|
|
||||||
// Get id field
|
// Get id field
|
||||||
obj.setFieldValue(bean_config.id_field, result.getLong(bean_config.id_field.getName()));
|
obj.id = id;
|
||||||
// Get the rest
|
// Get the rest
|
||||||
for( Field field : bean_config.fields ){
|
for( Field field : bean_config.fields ){
|
||||||
// Skip id field (its already loaded)
|
|
||||||
if( field.equals(bean_config.id_field) )
|
|
||||||
continue;
|
|
||||||
String name = field.getName();
|
String name = field.getName();
|
||||||
|
|
||||||
// Another DBBean class
|
// Another DBBean class
|
||||||
|
|
@ -162,7 +159,7 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
||||||
// Load list from link table
|
// Load list from link table
|
||||||
PreparedStatement subStmt = db.getPreparedStatement("SELECT * FROM "+subtable+" WHERE ?=?");
|
PreparedStatement subStmt = db.getPreparedStatement("SELECT * FROM "+subtable+" WHERE ?=?");
|
||||||
subStmt.setString(1, idcol);
|
subStmt.setString(1, idcol);
|
||||||
subStmt.setObject(2, bean_config.id_field.get( obj ));
|
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);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue