1

我有 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 类型,它是我想要获取所有登录名的列表,但它不起作用。如何使用查询生成器来做到这一点?

4

1 回答 1

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

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

  • ORMLite 存储一个int其中LoginloginListID来自LoginsList. 当您将列表与 a 关联时Login,它必须已经在数据库中创建,因此它的 id 已经生成。

  • 如果您查看查询日志(使用以下 log4j 属性打开日志记录,您应该会看到关联的查询。您应该能够看到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
于 2012-10-24T12:54:52.440 回答