ORMLite Android和ForeignCollection索引1处的绑定值为null

时间:2014-09-25 06:55:03

标签: java android ormlite foreign-collection

我遇到了与ORMLite组织多对多关系的问题,这是我无法克服的。我有一个用户,并希望在他们之间建立友谊关系,所以我使用以下代码: 我有类User.java:

public class User {

    @DatabaseField(id = true, columnName = "_id")
    public long id;
    @ForeignCollectionField(columnName = COLUMN_FRIENDS, foreignFieldName = "user")
    public ForeignCollection<UserFriendship> friends;
}

然后我尝试在UserFriendship

的用户之间建立多对多的关系
@DatabaseTable
public class UserFriendship {
    public static final String USER_1_FIELD_NAME = "user";
    public static final String USER_2_FIELD_NAME = "friend";
    @DatabaseField(columnName = USER_1_FIELD_NAME, foreign = true, uniqueCombo = true)
    public  User        user;
    public  User friend;
    @DatabaseField(columnName = BaseColumns._ID, generatedId = true)
    private long        id;
}

我创建了一个简单的测试来检查一切正常:

public testUserFriendship() {
        User user = weClient.getEntityFactory().createUser();
        user.id = 101;
        user.nickName = "user";
        User friend = weClient.getEntityFactory().createUser();
        friend.id = 102;
        friend.nickName = "user2";

        user.friends.add(new UserFriendship(user, friend));
        dao.getUserDAO().createOrUpdate(user);
        dao.getUserDAO().createOrUpdate(friend);

        User user2;
        user2 = dao.getUserDAO().queryForId(user.id);

        assertEquals(user.friends.size(), user2.friends.size());
}

测试返回错误,这是一个回溯:

java.lang.IllegalArgumentException: the bind value at index 1 is null
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at com.j256.ormlite.android.compat.JellyBeanApiCompatibility.rawQuery(JellyBeanApiCompatibility.java:21)
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at com.j256.ormlite.android.AndroidCompiledStatement.getCursor(AndroidCompiledStatement.java:180)
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at com.j256.ormlite.android.AndroidCompiledStatement.runQuery(AndroidCompiledStatement.java:65)
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at com.j256.ormlite.stmt.SelectIterator.<init>(SelectIterator.java:55)
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:247)
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at com.j256.ormlite.dao.BaseDaoImpl.createIterator(BaseDaoImpl.java:964)
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at com.j256.ormlite.dao.BaseDaoImpl.iterator(BaseDaoImpl.java:533)
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at com.j256.ormlite.dao.LazyForeignCollection.seperateIteratorThrow(LazyForeignCollection.java:313)
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at com.j256.ormlite.dao.LazyForeignCollection.iteratorThrow(LazyForeignCollection.java:71)
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at com.j256.ormlite.dao.LazyForeignCollection.closeableIterator(LazyForeignCollection.java:60)
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at com.j256.ormlite.dao.LazyForeignCollection.iterator(LazyForeignCollection.java:47)
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at com.j256.ormlite.dao.LazyForeignCollection.size(LazyForeignCollection.java:106)
09-25 10:39:26.509    8101-8114/? W/System.err﹕ at co.wecommunicate.test.ORMTest.testUserFriendship(ORMTest.java:179)enter code here

表中的数据显示没有问题:

sqlite> select * from user;
|user|0|101|0.0|0.0|0|0|0|0
|user2|0|102|0.0|0.0|0|0|0|0
sqlite> select * from userfriendship;
102|101|1

当我开始调试库时,我找到了

0 个答案:

没有答案