简体   繁体   中英

GreenDAO ToOne relationship not working

I am implementing a User class as:

@Entity
public class User implements Serializable {

    /** Serial version UID */
    static final long serialVersionUID = 200L;

    @Id
    /** The user system id */
    private long id;

    @Index(unique = true)
    /** The user login username*/
    private String username;

    /** The user first name */
    private String firstName;

    /** The user last name*/
    public String lastName;

    @ToOne
    @NotNull
    /** The company this user belongs*/
    private Company company;

   ...
}

and Company

@Entity
public class Company extends JsonModel implements Serializable {

    /** Serial version UID */
    static final long serialVersionUID = 100L;

    @Id
    /** The sqlite record id */
    private long id;


    @NotNull
    /** The company public name */
    private String name;

...

}

The @ToOne should be working all right because I can see the generated UserDao with TABLE instruction:

public final static Property Company = new Property(6, long.class, "company", false, "COMPANY");

During the app flow I create an User instance (not db) and set a Company object to it.

Then I call:

UserDao userDao = daoSession.getUserDao();
userDao.insert(user);

However my app is failing specifying that Company cannot be null. When I open the UserDao generated class I see that bindValues never creates a Company before saving the user.

 @Override
    protected final void bindValues(DatabaseStatement stmt, User entity) {
        stmt.clearBindings();
        stmt.bindLong(1, entity.getDbId());
        stmt.bindLong(2, entity.getId());

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

        String firstName = entity.getFirstName();
        if (firstName != null) {
            stmt.bindString(4, firstName);
        }

        String lastName = entity.getLastName();
        if (lastName != null) {
            stmt.bindString(5, lastName);
        }
    }

What do I have to do in order to have the child entities created?

EDIT

OK I just read that relationships are not managed automatically by ORM. I am not seeing many benefits on using the lib hehe..

OK I just read that relationships are not managed automatically by ORM.

So I should insert the company before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM