如何从@relation获取数据(如果有孩子的情况) [英] How to get data from @relation where child condition

查看:42
本文介绍了如何从@relation获取数据(如果有孩子的情况)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用@Relation从一对一的关系中获取数据,但要针对孩子的情况

I want to get data from One to One Relationship using @Relation but with condition for the child

@Entity
public class User {
    @PrimaryKey public long userId;
    public String name;
    public int age;
}

@Entity
public class Library {
    @PrimaryKey public long libraryId;
    public long userOwnerId;
    public String code;

}

public class UserAndLibrary {
    @Embedded public User user;
    @Relation(
         parentColumn = "userId",
         entityColumn = "userOwnerId"
    )
    public Library library;
}

@Transaction
@Query("SELECT * FROM User") // I want to get where library.code = "123"
public UserAndLibrary getUsersAndLibraries();

我想要的数据是来自用户的所有数据,其中library.code ="123"我知道我可以使用@Embedded,但是我的数据使用许多相同的名称,并且我不想使用前缀.所以我想知道如何使用@Relation

Data that I want is All data From User where library.code = "123" I know I can use @Embedded, but my data use many same name and I don't want to use prefix. So I wonder how to do this using @Relation

谢谢

推荐答案

使用@Relation不能将"child"设置为状况.因此,如果您不想处理表的联接,则选择-替换 UserAndLibrary 中的父/子类:

With @Relation you cannot set "child" condition. So your choice if you don't want to deal with tables' joins - is to replace your parent/child classes in UserAndLibrary:

public class UserAndLibrary {
    @Embedded public Library library;
    @Relation(
         parentColumn = "userOwnerId",
         entityColumn = "userId"
    )
    public User user;
}

和道:

@Transaction
@Query("SELECT * FROM library Where code = :code") // now you can set condition
public UserAndLibrary getUsersAndLibraries(String code);

这篇关于如何从@relation获取数据(如果有孩子的情况)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆