2020import android .database .Cursor ;
2121import android .database .sqlite .SQLiteDatabase ;
2222
23- import com .activeandroid .annotation .Column ;
2423import com .activeandroid .content .ContentProvider ;
2524import com .activeandroid .query .Delete ;
2625import com .activeandroid .query .Select ;
@@ -37,17 +36,17 @@ public abstract class Model {
3736 // PRIVATE MEMBERS
3837 //////////////////////////////////////////////////////////////////////////////////////
3938
40- @ Column (name = "Id" )
4139 private Long mId = null ;
4240
43- private TableInfo mTableInfo ;
44-
41+ private final TableInfo mTableInfo ;
42+ private final String idName ;
4543 //////////////////////////////////////////////////////////////////////////////////////
4644 // CONSTRUCTORS
4745 //////////////////////////////////////////////////////////////////////////////////////
4846
4947 public Model () {
5048 mTableInfo = Cache .getTableInfo (getClass ());
49+ idName = mTableInfo .getIdName ();
5150 }
5251
5352 //////////////////////////////////////////////////////////////////////////////////////
@@ -59,7 +58,7 @@ public final Long getId() {
5958 }
6059
6160 public final void delete () {
62- Cache .openDatabase ().delete (mTableInfo .getTableName (), "Id =?" , new String [] { getId ().toString () });
61+ Cache .openDatabase ().delete (mTableInfo .getTableName (), idName + " =?" , new String [] { getId ().toString () });
6362 Cache .removeEntity (this );
6463
6564 Cache .getContext ().getContentResolver ()
@@ -150,7 +149,7 @@ else if (ReflectionUtils.isSubclassOf(fieldType, Enum.class)) {
150149 mId = db .insert (mTableInfo .getTableName (), null , values );
151150 }
152151 else {
153- db .update (mTableInfo .getTableName (), values , "Id =" + mId , null );
152+ db .update (mTableInfo .getTableName (), values , idName + " =" + mId , null );
154153 }
155154
156155 Cache .getContext ().getContentResolver ()
@@ -161,11 +160,13 @@ else if (ReflectionUtils.isSubclassOf(fieldType, Enum.class)) {
161160 // Convenience methods
162161
163162 public static void delete (Class <? extends Model > type , long id ) {
164- new Delete ().from (type ).where ("Id=?" , id ).execute ();
163+ TableInfo tableInfo = Cache .getTableInfo (type );
164+ new Delete ().from (type ).where (tableInfo .getIdName ()+"=?" , id ).execute ();
165165 }
166166
167167 public static <T extends Model > T load (Class <T > type , long id ) {
168- return (T ) new Select ().from (type ).where ("Id=?" , id ).executeSingle ();
168+ TableInfo tableInfo = Cache .getTableInfo (type );
169+ return (T ) new Select ().from (type ).where (tableInfo .getIdName ()+"=?" , id ).executeSingle ();
169170 }
170171
171172 // Model population
@@ -232,7 +233,7 @@ else if (ReflectionUtils.isModel(fieldType)) {
232233
233234 Model entity = Cache .getEntity (entityType , entityId );
234235 if (entity == null ) {
235- entity = new Select ().from (entityType ).where ("Id =?" , entityId ).executeSingle ();
236+ entity = new Select ().from (entityType ).where (idName + " =?" , entityId ).executeSingle ();
236237 }
237238
238239 value = entity ;
0 commit comments