How to work with nested relationships in Room

前端 未结 1 1134
再見小時候
再見小時候 2021-02-02 00:18

I have entities:

@Entity
public class A {
    @PrimaryKey(autoGenerate = true)
    public long id;
    public A() {}
}

@Entity()
public class B {
    @PrimaryKe         


        
相关标签:
1条回答
  • 2021-02-02 00:49

    It seems that you can have nested relations (the Javadoc on documentation page is for some reason not showing the whole code and is misleading for that reason).

    It is working:

    public class AWithB {
        @Embedded
        public A a;
    
        @Relation(parentColumn = "id", entityColumn = "aId", entity = B.class)
        public List<BWithC> bWithC;
    
        public AWithB() {}
    }
    

    For relations Many To One you can still use @Relation annotation. For some reason you cannot have simple instance - you need a collection here. But it is working:

    public class BWithC {
        @Embedded
        public B b;
        @Relation(parentColumn = "oneCId", entityColumn = "id")
        public Set<C> oneC;
        @Relation(parentColumn = "anotherCId", entityColumn = "id")
        public Set<C> anotherC;
    
        public BWithC() {}
    }
    
    0 讨论(0)
提交回复
热议问题