多对多在连接表中对数字进行排序

时间:2014-04-26 09:09:47

标签: android ormlite

我有2个课程,需要多对多的关系。第一类(Connection)聚合我的订单定义的第二类(City)(看Connection<City>号)。

问题是,当我获得所有连接并设置路径(Collection<City>)时,路径中的顺序是城市ID而不是数字。

以下是我的课程:

@DatabaseTable
public class Connection implements Serializable {    
    @DatabaseField(generatedId = true, columnName = ID)
    private int mId;
    private Collection<City> mPath;

    public void setPath(List<City> path) {
        mPath = path;
    }
}

Connection.java class

@DatabaseTable
public class City implements Serializable {
    @DatabaseField(generatedId = true, columnName = ID)
    private int mId;
    @DatabaseField(columnName = NAME, unique = true, canBeNull = false)
    private String mName;
}

City.java class

@DatabaseTable
public class ConnectionCity {
    public static final String CONNECTION = "cc_connection";
    public static final String CITY = "cc_city";
    public static final String NUMBER = "cc_number";

    @DatabaseField(generatedId = true)
    private int mId;
    @DatabaseField(foreign = true, canBeNull = false, columnName = CONNECTION)
    private Connection mConnection;
    @DatabaseField(foreign = true, canBeNull = false, columnName = CITY)
    private City mCity;
    @DatabaseField(canBeNull = false, columnName = NUMBER)
    private int mNumber; // I want to order by this
}

ConnecionCity.java class

DatabseHelper

public Collection<Connection> getAllConnections() {
    try {
        final List<Connection> connections = mConnectionDao.queryForAll();

        for (Connection connection : connections) {
            List<City> path = lookUpForCities(connection);

            connection.setPath(path);
        }

        return connections;
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

private List<City> lookUpForCities(Connection connection) throws SQLException {
    PreparedQuery<City> postsForUserQuery = makeCitiesForConnectionQuery();
    postsForUserQuery.setArgumentHolderValue(0, connection);
    return mCityDao.query(postsForUserQuery);
}

private PreparedQuery<City> makeCitiesForConnectionQuery() throws SQLException {

    // build our inner query for UserPost objects
    QueryBuilder<ConnectionCity, Integer> connectionCityQb = mConnectionCityDao.queryBuilder();

    // just select the post-id field
    connectionCityQb.selectColumns(ConnectionCity.CITY);
    SelectArg userSelectArg = new SelectArg();

    // you could also just pass in user1 here
    connectionCityQb.where().eq(ConnectionCity.CONNECTION, userSelectArg);
    connectionCityQb.orderBy(ConnectionCity.NUMBER, true);

    // build our outer query for Post objects
    QueryBuilder<City, Integer> cityQb = mCityDao.queryBuilder();

    // where the id matches in the post-id from the inner query
    cityQb.where().in(City.ID, connectionCityQb);

    return cityQb.prepare();
}

DatabaseHeler.java class

0 个答案:

没有答案