如何在Android中使用Room Persistance Library查询嵌套的嵌入对象? [英] How to query nested Embeded objects using Room Persistance Library in android?

查看:267
本文介绍了如何在Android中使用Room Persistance Library查询嵌套的嵌入对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到我有3个类,用户,地址,位置

Consider I have 3 classes User, Address, Location

class Address {
    public String street;
    public String state;
    public String city;

    @ColumnInfo(name = "post_code")
    public int postCode;

    @Embedded(prefix = "home_")
    public Location homeLocation;

    @Embedded(prefix = "office_")
    public Location officeLocation;
}

class Location{
     public long lat;
     public long lng;
}

@Entity
class User {
    @PrimaryKey
    public int id;

    public String firstName;

    @Embedded(prefix = "addr_")
    public Address address;
}

我应该如何编写查询以获取其家庭位置在特定纬度和经度边界之间的用户?

How should i write the query to get the users whose home location is between certain latitude and longitude boundary ?

例如:如果我想查找其住所位置在这两点之间的所有用户,则Location1(13.135795,77.360348)&位置2(12.743639,77.901424). 我的查询看起来像这样-

Ex: If i want to find all users whose home location is between these two points Location1(13.135795,77.360348) & Location2(12.743639, 77.901424). My query would look something like this -

从User中选择*,其中address.homelocation.lat< :l1_latitude&& address.homelocation.lat> l2_latitude& address.homelocation.lng>:l1_longitude&& address.homelocation.lng< :l2_longitude

select * from User where address.homelocation.lat < :l1_latitude && address.homelocation.lat > l2_latitude && address.homelocation.lng > :l1_longitude && address.homelocation.lng < :l2_longitude

如果根据我的理解,如果我必须在嵌入位置使用前缀,请纠正我是否有误,address内的所有字段都将附加前缀.因此,我可以将城市查询为addr_city,如果我必须在homeLocation内查询lat,那么它将变成addr_home_lat吗?

If i have to use prefix in the embedded location from my understanding, correct me if am wrong, all the fields inside address will get appended with prefix. So i can query city as addr_city and if i have to query lat inside the homeLocation then will it become addr_home_lat ?

房间数据库中是否允许嵌套的嵌入式对象?如果是,那么我该如何查询嵌套的嵌入式对象?

Is nested embedded objects permitted in room database? If yes then how do i query the nested embedded objects?

这里需要一些帮助.谢谢你.

Need some help here. Thank you.

推荐答案

是的,在ROOM中允许使用嵌套的嵌入式对象".您可以编写一个具有嵌入式Address类的User类,其中包含一个嵌入式Location类.

Yes "nested embedded objects" are permitted inside ROOM. You can write a User class which has an embedded Address class as which contains a embedded Location class.

每次添加嵌入式对象时,房间都会使桌子变平.在您的情况下,房间会生成一个名为"User"的表,其中包含以下各列:

Each time when an embedded object is added, room flattens out the table. In your case room generates a table called "User" with the following columns:

id,名字,addr_street,addr_state,addr_city,addr_post_code,addr_home_lat,addr_home_lng,addr_office_lat,addr_office_lng

id, firstName, addr_street, addr_state, addr_city, addr_post_code, addr_home_lat, addr_home_lng, addr_office_lat, addr_office_lng

因此您的查询应类似于:

So your query should be like :

@Query("SELECT * FROM User WHERE " +
        "addr_home_lat BETWEEN :lat1 AND :lat2" +
        " AND addr_home_lng BETWEEN :lng1 AND :lng2")
List<User> findInRange(long lat1, long lat2, long lng1, long lng2);

请注意,"lat"已展平为"addr_home_lat","lng"已展平为"addr_home_lng".因此,您可以使用这些列名称来应用过滤器逻辑.

Notice that "lat" has been flattened to "addr_home_lat" and "lng" to "addr_home_lng". So you can use these column names for applying the filter logic.

万一您拼写错误的列名(例如"home_lng"而不是"addr_home_lng"),则房间会发现该错误并给您错误:

In case you misspelled a column name for example "home_lng" instead of "addr_home_lng", then room will find that out and give you an error :

There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such column: home_lng)

有关会议室数据库的更多信息,请查看 Google I/O 谈话.

For more information on room database, check out the Google I/O talk.

这篇关于如何在Android中使用Room Persistance Library查询嵌套的嵌入对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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