greenrobot / greenDAO

greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases.
http://greenrobot.org/greendao/
12.63k stars 2.89k forks source link

ToOne join property not automatically created #767

Open perqin opened 7 years ago

perqin commented 7 years ago

Simply put, I have two entities Live and Progress, and each Live has a Progress. Here's my entity classes (generated code, static constants and transient fields are ignored):

import com.wsclass.wsclassteacher.data.models.jobs.DaoSession;
import com.wsclass.wsclassteacher.data.models.jobs.Progress;
import com.wsclass.wsclassteacher.data.models.jobs.ProgressDao;

import org.greenrobot.greendao.DaoException;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.ToOne;
import org.greenrobot.greendao.annotation.Transient;

import java.util.ArrayList;

import lombok.Getter;
import lombok.Setter;

@Entity(nameInDb = "lives", generateGettersSetters = false)
@Getter
@Setter
public class Live {
    @Id(autoincrement = true)
    private Long _id;

    private String roomId;

    private String name;

    private Integer status;

    @ToOne
    private Progress uploadProgress;
}
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;

import lombok.Getter;
import lombok.Setter;

@Entity(nameInDb = "live_progresses", generateGettersSetters = false)
@Getter
@Setter
public class Progress {
    @Id(autoincrement = true)
    private Long _id;

    private Integer finished;

    private Integer total;
}

The problematic code is:

Live fetched = new Live();
// Statements to set other fields in Live
// ...
Progress progress = new Progress();
progress.setFinished(0);
progress.setTotal(0);
progressDao.insert(progress);
fetched.setUploadProgress(progress);
liveDao.insert(fetched);

The problem is that the id of Progress are not included when the new Live is inserted. I've pulled out the SQLite file from my device and opened it. I found that the UPLOAD_PROGRESS column is null.

I think this is due to incorrectly generated DAO classes. Here's the bindValues method generated in LiveDao class:

    @Override
    protected final void bindValues(SQLiteStatement stmt, Live entity) {
        stmt.clearBindings();

        Long _id = entity.get_id();
        if (_id != null) {
            stmt.bindLong(1, _id);
        }

        String roomId = entity.getRoomId();
        if (roomId != null) {
            stmt.bindString(2, roomId);
        }

        String name = entity.getName();
        if (name != null) {
            stmt.bindString(3, name);
        }

        Integer status = entity.getStatus();
        if (status != null) {
            stmt.bindLong(4, status);
        }
    }

You can see that the uploadProgress is not bound to the insert SQLite statement.

greenrobot-team commented 7 years ago

Could reproduce, thanks. As it apparently does not work automatically, try to explicitly add the join property for Progress like:

private Long uploadProgressId;

@ToOne(joinProperty = "uploadProgressId")
private Progress uploadProgress;

Then rebuild your project.

Note though, that greenDAO requires special implementations for the getUploadProgress() and setUploadProgress() methods. So temporarily remove the generateGettersSetters flag to generate them. -ut

davidcabezas commented 6 years ago

I'm facing the same issue. I tried the Order-Customer example you provide, but the query always returns the @ToOne property as NULL. Do you have any valid project example showing how it should be done?

softboy99 commented 5 years ago

I'm facing the same issue