Adam
Adam

Reputation: 26497

ORMLite foreign member updates

I have a Top level element that I'm saving to the database, and it has several foreign elements, something like this:

@DatabaseTable
public class Parent {
    @DatabaseField(id = true, index = true)
    public Integer id;

    @DatabaseField(foreign = true)
    public ChildA a;
}

@DatabaseTable
public class ChildA {
    DatabaseField(generatedId = true, index = true)
    public Integer id;

    @DatabaseField
    public boolean something;
}

Assuming these have already been created in the database. And now I want to update them. Will calling parentDao.update(parent) update both? Or do I need to manually update the child as well?

Upvotes: 4

Views: 1095

Answers (1)

Gray
Gray

Reputation: 116908

The short answer is:

no, it won't update both

Foreign objects are not proxy objects so there is no way for ORMLite to determine if the sub-object has been modified and needs to be updated. So if you change both the Parent and ChildA objects then you'd have to do something like:

 childADao.update(parent.a);
 parentDao.update(parent);

Obviously, if you set a new ChildA on parent then it will update this new id in the parent table.

Upvotes: 7

Related Questions