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

标签 java android android-room

我目前正在构建一个 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/

相关文章:

java - 如何将具有一对一关系 pojo(ROOM 实现)和 safeArgs 的 List<> 传递到目的地

java - 求和文本文件中整数的最快方法

java - org/apache/cxf/jaxb/JAXBToStringStyle 上的 NoClassDefFoundError

java - JScrollPane 没有显示?

android - 简单的导航动画与android

android - 具有一对一关系的房间数据库

java - 在 Android Room 数据库中存储 List<Object>

java - 从java程序中出来

Android:发布草稿 APK 和应用内结算测试 - 如何?

android - 在 jquery/javascript 中复制到剪贴板,没有 ipad/iphone 的 flash