gpt4 book ai didi

java - 使用 Android Room 从 Junction Table 获取数据

转载 作者:行者123 更新时间:2023-12-01 16:24:28 26 4
gpt4 key购买 nike

我目前正在构建一个 Android 应用程序,它显示一条路线,该路线由多个路径点构建而成。我已经规划了数据库模式(chen-notation [可能无效的“语法”]):

er-model

我尝试重新创建与 android room 的 n-m 关系,但我不知道如何检索联结表 (route_waypoint) 的 index_of_route 属性。

当我像这样获取数据时,我想要联结表属性index_of_route:

    @Transaction
@Query("SELECT * FROM POIRoute")
List<RouteWithWaypoints> getRoutes();

在 POIWaypoint 类中(可能作为额外属性),或者至少可以从另一个类访问,该类可能是这样实现的:

    @Embedded
POIWaypoint waypoint;
int indexOfRoute;

目前我没有从联结表中获取 indexOfRoute 属性。

我已经创建的类:

带航点的路线:

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;
}

路线航点:

@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;
}
}

POIR路线:

@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(请忽略尚未完成的位置属性):

@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 更改为抽象类来插入我自己的方法,该方法自己管理部分联结表:

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:

    @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);

关于java - 使用 Android Room 从 Junction Table 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62178333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com