ORMLite查询外键

时间:2012-10-24 11:16:29

标签: android database foreign-keys ormlite

我有2个班级

列表

@DatabaseTable(tableName = "loginsList")
public class LoginsList {

    @DatabaseField(columnName = "loginListID", generatedId = true)
    private int loginListID;
    @DatabaseField(columnName = "listName")
    private String listName;
    @DatabaseField(columnName = "listDescription")
    private String listDescription;
    @DatabaseField(columnName = "current")
    private boolean current;
    @DatabaseField(columnName = "sent")
    private boolean sent;

登录

@DatabaseTable(tableName = "login")
public class Login {
    @DatabaseField(columnName = "loginID", generatedId = true)
    private int loginID;
    @DatabaseField(columnName = "user", foreign=true)
    private User user;
    @DatabaseField(columnName = "loggedIn")
    private boolean loggedIn;
    @DatabaseField(columnName = "loginTime")
    private long loginTime;
    @DatabaseField(columnName = "logoutTime")
    private long logoutTime;
    @DatabaseField(columnName = "loginsList", foreign = true)
    private LoginsList loginsList;

我使用ORMLite作为数据库。我想知道如何获取列表的所有登录信息?我试过这个

tempLoginList = loginDao.queryForEq("loginsList", list);

其中list是LoginsList类型,它是我想要获取所有登录的列表,但它不起作用。如何使用查询构建器执行此操作?

1 个答案:

答案 0 :(得分:2)

tempLoginList = loginDao.queryForEq("loginsList", list);

这应该可以正常工作。这里有几件事要检查,可能会妨碍你。

  • ORMLite在int中存储Login,这是loginListID中的LoginsList字段。将列表与Login关联时,必须已在数据库中创建,因此已生成其ID。

  • 如果您查看查询日志(使用以下log4j属性打开logging,您应该会看到相关的查询。您应该能够看到login id字段被查询。你需要确保它是正确的。

    log4j.logger.com.j256.ormlite=DEBUG
    # to enable logging of arguments to all of the SQL calls
    # uncomment the following lines
    log4j.logger.com.j256.ormlite.stmt.mapped.BaseMappedStatement=TRACE
    #log4j.logger.com.j256.ormlite.stmt.mapped.MappedCreate=TRACE
    #log4j.logger.com.j256.ormlite.stmt.StatementExecutor=TRACE
    

我有很多单元测试,但没有具体围绕dao.queryForEq(...)和外国字段。我刚刚将它们添加到代码库中,它们工作正常。这是我的日志输出,可能会有所帮助:

[DEBUG] BaseMappedStatement insert data with statement 'INSERT INTO `foo` (`val` ,`equal` ,`null` ) VALUES (?,?,?)' and 3 args, changed 1 rows
[TRACE] BaseMappedStatement insert arguments: [1231231, 0, null]
[DEBUG] BaseMappedStatement assigned id '1' from keyholder to 'id' in Foo object
[DEBUG] BaseMappedStatement insert data with statement 'INSERT INTO `foreign` (`foo_id` ) VALUES (?)' and 1 args, changed 1 rows
[TRACE] BaseMappedStatement insert arguments: [1]
[DEBUG] BaseMappedStatement assigned id '1' from keyholder to 'id' in Foreign object
[DEBUG] StatementBuilder built statement SELECT * FROM `foreign` WHERE `foo_id` = 1 
[DEBUG] BaseMappedStatement prepared statement 'SELECT * FROM `foreign` WHERE `foo_id` = 1 ' with 0 args
[DEBUG] SelectIterator starting iterator @1781793025 for 'SELECT * FROM `foreign` WHERE `foo_id` = 1 '
[DEBUG] SelectIterator closed iterator @1781793025 after 1 rows
[DEBUG] StatementExecutor query of 'SELECT * FROM `foreign` WHERE `foo_id` = 1 ' returned 1 results