1

我有两个像这样的简单类:

@DatabaseTable(tableName = "movements")
public class Movement {

    @DatabaseField(generatedId = true)
    int id;

    @DatabaseField
    double amount;

    @DatabaseField(foreign = true)
    Category category;

    @ForeignCollectionField(eager = true)
    Collection<Tag> tags;

    //Other stuff
}

第二个:

@DatabaseTable(tableName = "tag")
public class Tag {

    @DatabaseField(generatedId = true)
    int id;

    @DatabaseField(foreign = true)
    Movement movement;

    @DatabaseField
    String name;

    //Other stuff
}

我对以下两个命令的期望:

TableUtils.createTable(dbConnection, Tag.class);
TableUtils.createTable(dbConnection, Movement.class);

是在数据库中创建两个表,并且对另一个表具有正确的引用。Insean 似乎忽略了“运动”类中的 ForeignCollection。这可能是调试信息:

[INFO] TableUtils creating table 'tag'
[INFO] TableUtils executed create table statement changed 0 rows: CREATE TABLE `tag` (`id` INTEGER AUTO_INCREMENT , `movement_id` INTEGER , `name` VARCHAR(255) , PRIMARY KEY (`id`) ) 
[INFO] TableUtils creating table 'movements'
[INFO] TableUtils executed create table statement changed 0 rows: CREATE TABLE `movements` (`id` INTEGER AUTO_INCREMENT , `amount` DOUBLE PRECISION , `category_id` INTEGER , PRIMARY KEY (`id`)

我看不到我的错误!

4

1 回答 1

1

Insean 似乎忽略了“运动”类中的 ForeignCollection。

是的,这有点令人困惑,但没有错误。表中没有movements引用tags数据库中的任何内容。

外国集合在 ORMLite 中的工作方式(如休眠和其他 ORM)是 atag具有对关联的引用movement_id,但不是相反。根据定义, Amovement不能有某种tagid 列表。这就是 ORMLite 使用另一个查询来填写tags集合的原因。它返回您的movement,然后idmovement中查找以查找在该movement_id字段中具有相同的标签。

于 2016-01-17T20:59:27.257 回答