2017-05-07 25 views
0

我使用OrmLite 5.0。我創建了2個實體,其中一個與另一個實體是一對一的關係。 這是我的回合類:OrmLite作爲外​​部文件的字段返回null,除了id字段

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-- 
################################# 

我堅持比賽而圓。

​​

回報

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]] 

我不知道如何從輪得到充分的競爭對象。

回答

0

我解決了這個問題在我的情況。 我的代碼是:

@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 =真正的」我收到的對象與剛剛初始化的ID異物驅動程序。 當我添加「whithout foreignAutoRefresh = true」,異物填充其所有文件包括id。 如果您刪除「foreignAutoCreate = true」可能會修復您的問題。