0

現在のプロジェクトでは、ORMLite (4.46) を使用することにしました。現在、異物のデータを保存する際にいくつかの問題が発生しています。以下、私のコードの例。「フィールド」というフィールドが正しく保存されていません(古いデータが表示されます)。このコードの何が問題なのですか?

ありがとうございました!

よろしくアンドレアス

いくつかのクラス:

DatabaseHelper databaseHelper = OpenHelperManager.getHelper(context, DatabaseHelper.class)
branch.field = 123423;
Log.d("...", "field:" + branch.field); <-- new value of field is displayed
databaseHelper.getBranchDao().update(branch);

他のクラス:

DatabaseHelper databaseHelper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
databaseHelper.getFloorDao().refresh(floor);
databaseHelper.getBranchDao().refresh(floor.branch);
Log.d("...", "branch field:" + floor.branch.field); <-- old value of field is displayed

データベースヘルパー:

...

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

    // name of the database file for your application -- change to something appropriate for your app
    private static final String DATABASE_NAME = "xy.db";
    // any time you make changes to your database objects, you may have to increase the database version
    private static final int DATABASE_VERSION = 1;

    // the DAO objects we use to access the tables
    private Dao<Branch, Integer> branchDao = null;
    private Dao<Floor, Integer> floorDao = null;

    private Context appContext;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);

        appContext = context;
    }

    /**
     * This is called when the database is first created. Usually you should call createTable statements here to create
     * the tables that will store your data.
     */
    @Override
    public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
        try {
            Log.i(DatabaseHelper.class.getName(), "onCreate");
            // Create tables for each domain object here!
            TableUtils.createTable(connectionSource, Branch.class);
            TableUtils.createTable(connectionSource, Floor.class);
        } catch (SQLException e) {
            Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * This is called when your application is upgraded and it has a higher version number. This allows you to adjust
     * the various data to match the new version number.
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            Log.i(DatabaseHelper.class.getName(), "onUpgrade");
            // Drop all tables of domain objects here!
            TableUtils.dropTable(connectionSource, Branch.class, true);
            TableUtils.dropTable(connectionSource, Floor.class, true);
            // after we drop the old databases, we create the new ones
            onCreate(db, connectionSource);
        } catch (SQLException e) {
            Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * Delete and re-create database when user is changed
     */
    public void reCreateDB(){
        //delete the database (onCreate() will be called if database is needed again)
        appContext.deleteDatabase(DATABASE_NAME);
    }

    /**
     * Returns the Database Access Object (DAO) for our domain class. It will create it or just give the cached
     * value.
     */


    public Dao<Branch, Integer> getBranchDao() throws SQLException {
        if (branchDao == null) {
            branchDao = getDao(Branch.class);
        }
        return branchDao;
    }

    public Dao<Floor, Integer> getFloorDao() throws SQLException {
        if (floorDao == null) {
            floorDao = getDao(Floor.class);
        }
        return floorDao;
    }



    /**
     * Close the database connections and clear any cached DAOs.
     */
    @Override
    public void close() {
        super.close();
    }
}

床:

...

public class Floor {
    @DatabaseField(generatedId = true)
    public int id;

    @DatabaseField(foreign = true, canBeNull = true, foreignAutoRefresh = true, maxForeignAutoRefreshLevel=5)
    public Branch branch;

    public Floor(){}
}

ブランチ:

....

public class Branch {

    @DatabaseField(generatedId = true)
    public int id;
    @DatabaseField(canBeNull = true)
    public int field;

    @ForeignCollectionField(eager = true, maxEagerLevel = 99)
    private ForeignCollection<Floor> floors;

    public Branch(){}

    public ForeignCollection<Floor> getEmptyFloorCollection() {
        try {
            return MainActivity.getDatabaseHelper().getBranchDao().getEmptyForeignCollection("floors");
        } catch (SQLException e) {
            Log.e("DatabaseHelper", "SQL Exception: " + e.getLocalizedMessage());
            return null;
        }
    }

    public ForeignCollection<Floor> getFloors() {
        if(floors == null) floors = getEmptyFloorCollection();
        return floors;
    }

    public void setFloors(ForeignCollection<Floor> floors) {
        this.floors = floors;
    }
}
4

0 に答える 0