lassa
lassa

Reputation: 173

OrmLite returns null as foreign fileds' fields except id field

I use OrmLite 5.0. I created 2 entities, one is in relation one-to-one with another. This is my Round class:

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() {
}

and this is my Competition class:

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() {
}

this is my 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--
#################################

I persisted Competition and Round.

compeitionDAO.queryForAll();

returns

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

but

roundDAO.queryForAll();

returns

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

I have no idea how to get full competition object from round.

Upvotes: 0

Views: 525

Answers (2)

AmiNadimi
AmiNadimi

Reputation: 5715

From the docs,

2.12 Foreign Object Fields

When you are creating a field with a foreign object, please note that the foreign object will not automatically be created for you. If your foreign object has a generated-id which is provided by the database then you need to create it before you create any objects that reference it, . For example:

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);

If you want some level of auto creation, then you can use the foreignAutoCreate setting. See foreignAutoCreate.

you either have to set the full data foreign field for your entity somewhere before getting it, or you will have to use refresh.

to use foreign refresh functionality, either set foreignAutoRefresh to true or use the manual refreshing approach. see below for an example from docs :

When you query for an order, you will get an Order object with an account field object that only has its id field set. The rest of the fields in the foreign Account object will have default values (null, 0, false, etc.). If you want to use other fields in the Account, you must call refresh on the accountDao class to get the Account object filled in. For example:

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);

Upvotes: 0

Mahdi Moqadasi
Mahdi Moqadasi

Reputation: 2479

I solved this problem in my case. my code is:

@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() {
    }
}

when I queried for an object of this class "whithout foreignAutoRefresh = true" the object I received has a foreign object Driver with just an id initialized. when I added "whithout foreignAutoRefresh = true", foreign object filled all its filelds including id. May if you delete "foreignAutoCreate = true" your problem be fixed.

Upvotes: 0

Related Questions