Lazy ForeingCollection - what am I doing wrong?

29 views
Skip to first unread message

Dong

unread,
Feb 5, 2017, 8:39:50 AM2/5/17
to ORMLite Users

I have this ForeignCollection in one Entity:

 @ForeignCollectionField(eager = false)
   
public ForeignCollection<EntityLog> logs;

When I add data to collection I use:

public EntityLog addLog(String log){
       
Date now=new Date();
       
EntityLog logE=new EntityLog(log, now);
        logy
.add(logE);
       
return logE;
   
}

    According to debug this correctly insert data to database.

However when I want to iterate over this collection.:

        ForeignCollection<EntityLog> logs = record.getLogs();
        try {
            logs .closeLastIterator();
        } catch (IOException ex) {
            Logger.getLogger(InfoDialogWorker.class.getName()).log(Level.SEVERE, null, ex);
        }
        CloseableIterator<EntityLog> iterator = logs.closeableIterator();
        try {
            while (iterator.hasNext()) {
                EntityLog next = iterator.next();
                this.logs.append(next.toString()+System.lineSeparator());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }finally {
            try {
                iterator.close();
            } catch (Exception ex) {
               ex.printStacktrace();
            }
        }

However iterator.next() is always false and according to debug parent ID is null:

2017-02-05 14:37:39,177 [DEBUG] BaseMappedStatement prepared statement 'SELECT * FROM `logs` WHERE `reocrd_id` = ? ' with 1 args
2017-02-05 14:37:39,177 [TRACE] BaseMappedStatement prepared statement arguments: [null]


Now when I restart software and load all Records (entity which contains this ForeingCollection) using querryForAll() then ID is not null anymore .. so one would think that parent ID is not populated but it is since I can easy see it in debugger and also when adding logs to this foreign collection correct insert statments are created with correct ID.


Dong

unread,
Feb 5, 2017, 8:41:46 AM2/5/17
to ORMLite Users
I forgot to add EntityLog class:

@DatabaseTable(tableName = "logs")
public class EntityLog extends BaseDaoEnabled  {
    @DatabaseField (generatedId = true)
    public long ID;
    
    @DatabaseField
    public String log;
    
     @DatabaseField
    public Date date;
    
    @DatabaseField(foreign = true,foreignAutoRefresh= true) 
    protected EntityRecord record;

    public EntityLog() {
    }

    public EntityLog(String log, Date datum) {
        this.log = log;
        this.datum = datum;
    } 

}
Reply all
Reply to author
Forward
0 new messages