【问题标题】:OrmLite returns null as foreign fileds' fields except id fieldOrmLite 返回 null 作为外部文件的字段,除了 id 字段
【发布时间】:2017-05-07 17:26:00
【问题描述】:

我使用 OrmLite 5.0。我创建了 2 个实体,一个与另一个是一对一的关系。 这是我的回合课:

public class Round {
@DatabaseField(id = true)
private int id;
@DatabaseField(canBeNull = false)
private String name;
@DatabaseField(canBeNull = false, foreign = true, foreignAutoRefresh = true, foreignAutoCreate = true)
private Competition competition;

public Round(int id, String name, Competition competition) {
    this.id = id;
    this.name = name;
    this.competition = competition;
}

public Round() {
}

这是我的竞赛课:

public class Competition {
@DatabaseField(id = true, columnName = "id", canBeNull = false)
private int id;
@DatabaseField(canBeNull = false)
private String name;
@DatabaseField(canBeNull = false)
private String flagUrl;

public Competition(int id, String name, String flagUrl) {
    this.id = id;
    this.name = name;
    this.flagUrl = flagUrl;
}

public Competition() {
}

这是我的 ormlite_config.txt

# --table-start--
dataClass=com.example.test.model.db.Competition
tableName=competition
# --table-fields-start--
# --field-start--
fieldName=id
id=true
# --field-end--
# --field-start--
fieldName=name
# --field-end--
# --field-start--
fieldName=flagUrl
# --field-end--
# --table-fields-end--
# --table-end--
#################################
#################################
# --table-start--
dataClass=com.example.test.model.db.Round
tableName=round
# --table-fields-start--
# --field-start--
fieldName=id
id=true
# --field-end--
# --field-start--
fieldName=name
# --field-end--
# --field-start--
fieldName=competition
columnName=competition_id
foreign=true
# --field-end--
# --table-fields-end--
# --table-end--
#################################

我坚持比赛和回合。

compeitionDAO.queryForAll();

返回

Competition[id=36, name='Champions League', flagUrl='https://static.crowdscores.com/flags/uefa.png'], 

但是

roundDAO.queryForAll();

返回

Round[id=1316, name="Group B", competition=Competition[id=36, name=null, flagUrl=null]]

我不知道如何从回合中获得完整的比赛对象。

【问题讨论】:

    标签: java android sql orm ormlite


    【解决方案1】:

    我解决了这个问题。 我的代码是:

    @DatabaseTable(tableName = "tbl_turn_list")
    public class TurnListItem {
        @DatabaseField(generatedId = true, index = true)
        public int id;
        @DatabaseField(defaultValue = "false")
        public Boolean is_sent;
        @DatabaseField(foreignAutoRefresh = true,foreign = true, columnName = "related_driver_id")
        public Driver related_driver;
    
        public TurnListItem() {
        }
    }
    

    当我查询此类“没有 foreignAutoRefresh = true” 的对象时,我收到的对象有一个外来对象 Driver,它只初始化了一个 id。 当我添加“没有foreignAutoRefresh = true”时,外来对象填充了它的所有filelds,包括id。 如果您删除 "foreignAutoCreate = true",您的问题可能会得到解决。

    【讨论】:

      【解决方案2】:

      来自文档,

      2.12 Foreign Object Fields

      当您使用外来对象创建字段时,请注意 不会自动为您创建外来对象。如果你的 外来对象有一个由数据库提供的生成 ID 那么您需要在创建任何对象之前创建它 参考一下,。例如:

      Account account = new Account("Jim Coakley");
      accountDao.create(account); 
      // this will create the account object and set any generated ids
      
      // now we can set the account on the order and create it 
      Order order = new Order("Jim Sanders", 12.34);
      order.setAccount(account); 
      …
      orderDao.create(order);
      

      如果您想要某种程度的自动创建,那么您可以使用 foreignAutoCreate 设置。见foreignAutoCreate

      您要么必须在获取实体之前在某处设置完整的数据外部字段,要么必须使用刷新。

      要使用外部刷新功能,请将 foreignAutoRefresh 设置为 true 或使用手动刷新方法。请参阅下面的文档示例:

      当您查询订单时,您将获得一个带有 只有 id 字段集的帐户字段对象。剩下的 外部 Account 对象中的字段将具有默认值(null, 0、假等)。如果您想使用帐户中的其他字段,您 必须在 accountDao 类上调用 refresh 来获取 Account 对象 填写。例如:

      Order order = orderDao.queryForId(orderId);
      System.out.println("Account-id on the order should be set: " +
         order.account.id);
      // this should print null for order.account.name
      System.out.println("But other fields on the account should not be set: "
         + order.account.name);
      
      // so we refresh the account using the AccountDao
      accountDao.refresh(order.getAccount());
      System.out.println("Now the account fields will be set: " +
         order.account.name);
      

      【讨论】:

      • @lassa 希望对您有所帮助,如果可以,请将其标记为答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-02
      • 2016-12-23
      相关资源
      最近更新 更多