使用Android Room从Junction Table获取数据 [英] Get data from Junction Table with Android Room
问题描述
我目前正在构建一个Android应用,该应用显示由多个航路点构成的Route.我已经计划了数据库模式(chen表示法[可能无效的语法"]):
I am currently building an android app, which displays a Route, which is constructed out of multiple waypoints. I already planned the database schema (chen-notation [possibly invalid "syntax"]):
我试图重新创建与Android房间的n-m关系,但是我不知道如何检索联结表(route_waypoint)的 index_of_route
属性.
I tried to recreate the n-m relation with android room, but I can't figure out how I can retrieve the index_of_route
attribute of the junction table (route_waypoint).
当我像这样获取数据时,我想要联结表属性 index_of_route
:
I want the junction table attribute index_of_route
, when I get the Data like so:
@Transaction
@Query("SELECT * FROM POIRoute")
List<RouteWithWaypoints> getRoutes();
在POIWaypoint类内部(可能是额外的属性),或者至少可以从另一个类进行访问,该类可以这样实现:
inside the POIWaypoint class (maybe as extra attribute), or at least accessible from another class which maybe is implemented like so:
@Embedded
POIWaypoint waypoint;
int indexOfRoute;
目前,我没有从联结表中获得 indexOfRoute
属性.
Currently I don't get the indexOfRoute
attribute from the junction table.
我已经创建的类:
RouteWithWaypoints:
RouteWithWaypoints:
public class RouteWithWaypoints {
@Embedded
private POIRoute poiRoute;
@Relation(parentColumn = "id",entityColumn = "id",associateBy = @Junction(value = RouteWaypoint.class, parentColumn = "routeId", entityColumn = "waypointId"))
private List<POIWaypoint> waypoints;
public POIRoute getPoiRoute() {
return poiRoute;
}
public void setPoiRoute(POIRoute poiRoute) {
this.poiRoute = poiRoute;
}
public List<POIWaypoint> getWaypoints() {
return waypoints;
}
public void setWaypoints(List<POIWaypoint> waypoints) {
this.waypoints = waypoints;
}
RouteWaypoint:
RouteWaypoint:
@Entity(primaryKeys = {"waypointId", "routeId"}, foreignKeys = {
@ForeignKey(entity = POIWaypoint.class, parentColumns = {"id"}, childColumns = {"waypointId"}),
@ForeignKey(entity = POIRoute.class, parentColumns = {"id"}, childColumns = {"routeId"})
})
public class RouteWaypoint {
private int waypointId;
private int routeId;
// I want this attribute inside the POIWaypoint class
@ColumnInfo(name = "index_of_route")
private int indexOfRoute;
public int getWaypointId() {
return waypointId;
}
public void setWaypointId(int waypointId) {
this.waypointId = waypointId;
}
public int getRouteId() {
return routeId;
}
public void setRouteId(int routeId) {
this.routeId = routeId;
}
}
POIRoute:
@Entity
public class POIRoute{
private String name;
private String description;
@PrimaryKey(autoGenerate = true)
private int id;
private boolean user_generated;
private int parentId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isUser_generated() {
return user_generated;
}
public void setUser_generated(boolean user_generated) {
this.user_generated = user_generated;
}
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
}
POIWaypoint(请忽略尚未完成的position属性):
POIWaypoint (please ignore the position attribute it isn't finished):
@Entity
public class POIWaypoint {
@PrimaryKey(autoGenerate = true)
private long id;
@ColumnInfo(name = "long_description")
private String longDescription;
private String title;
@ColumnInfo(name = "short_description")
private String shortDescription;
// use converter: https://developer.android.com/training/data-storage/room/referencing-data
@Ignore
private GeoPoint position;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public GeoPoint getPosition() {
return position;
}
public void setPosition(GeoPoint position) {
this.position = position;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
public String getLongDescription() {
return longDescription;
}
public void setLongDescription(String longDescription) {
this.longDescription = longDescription;
}
推荐答案
我通过自己管理关系解决了我的问题.我将RouteDao更改为一个抽象类,以插入我自己的方法,该方法自己管理联结表的一部分:
I solved my problem by manage the relation by myself. I changed my RouteDao to an abstract class to insert my own method, which manages part of the junction table by itself:
RouteDao:
private RouteDatabase database;
public RouteDao(RouteDatabase database) {
this.database = database;
}
@Query("Select * from POIRoute")
public abstract List<POIRoute> getRoutes();
@Query("SELECT * FROM POIRoute WHERE id = :id")
public abstract POIRoute getRoute(int id);
@Insert
abstract void insertRouteWithWaypoints(RouteWithWaypoints routeWithWaypoints);
public List<RouteWithWaypoints> getRoutesWithWaypoints() {
List<POIRoute> routes = this.getRoutes();
List<RouteWithWaypoints> routesWithWaypoints = new LinkedList<>();
for (POIRoute r : routes) {
routesWithWaypoints.add(new RouteWithWaypoints(r, database.wayPointDao().getWaypointsFromRoute(r.getId())));
}
return routesWithWaypoints;
}
public RouteWithWaypoints getRouteWithWaypoints(int id) {
POIRoute route = this.getRoute(id);
RouteWithWaypoints routeWithWaypoints = null;
if (route != null) {
routeWithWaypoints = new RouteWithWaypoints(route, database.wayPointDao().getWaypointsFromRoute(route.getId()));
}
return routeWithWaypoints;
}
WayPointDao:
WayPointDao:
@Query("SELECT * FROM POIWaypoint")
POIWaypoint getWaypoints();
@Query("SELECT * FROM POIWaypoint WHERE id = :id")
POIWaypoint getWaypoint(long id);
@Query("SELECT pw.*, rw.index_of_route as 'index' FROM POIWaypoint as pw Join RouteWaypoint as rw on (rw.waypointId = pw.id) where rw.routeId = :id order by 'index' ASC")
List<POIRouteStep> getWaypointsFromRoute(int id);
这篇关于使用Android Room从Junction Table获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!