在ORMLite for Android中加入类会引发SQL异常:找不到外来类,反之亦然

时间:2014-04-22 14:35:49

标签: java android sql ormlite

我正在尝试使用QueryBuilder为两个不同的类创建一个连接查询,一个Product类和一个Coupon类,它引用了一个Product属性storeId

public class Coupon {

    @DatabaseField(columnName = TableColumns.PRODUCT, foreign = true, foreignColumnName = Product.TableColumns.STOREID)
    private Product product;
}


public class Product {

    @DatabaseField(columnName = TableColumns.ID, generatedId = true)
    private Integer id;

    @DatabaseField(columnName = TableColumns.STOREID, index = true, unique = true)
    private Integer storeId;
}

我需要根据产品storeId获取优惠券列表。

public static List<Coupon> getByProduct(Product product) throws SQLException {
    QueryBuilder<Coupon, String> couponBuilder = dao.queryBuilder();
    QueryBuilder<Product, Integer> productBuilder = Product.dao.queryBuilder();     
    productBuilder.where().eq(Product.TableColumns.STOREID, product.getStoreId());
    return couponBuilder.join(productBuilder).query();
}

此查询引发SQL异常:

04-22 11:26:08.058: W/System.err(19479): java.sql.SQLException: Could not find a foreign class com.cde.express.mccopa.model.Coupon field in class com.cde.express.mccopa.model.Product or vice versa

04-22 11:26:08.059: W/System.err(19479):    at com.j256.ormlite.stmt.QueryBuilder.matchJoinedFields(QueryBuilder.java:554)

04-22 11:26:08.059: W/System.err(19479):    at com.j256.ormlite.stmt.QueryBuilder.addJoinInfo(QueryBuilder.java:525)

04-22 11:26:08.059: W/System.err(19479):    at com.j256.ormlite.stmt.QueryBuilder.join(QueryBuilder.java:316)

例外情况表明我的类与外地字段无关,但优惠券类具有外部Product属性,正确注释。我已经检查了数据库,表中的值是正确的。

我该如何解决?

1 个答案:

答案 0 :(得分:2)

查看QueryBuilder.java中的代码

       for (FieldType fieldType : tableInfo.getFieldTypes()) {
            // if this is a foreign field and its foreign-id field is the same as the other's id
            FieldType foreignIdField = fieldType.getForeignIdField();
            if (fieldType.isForeign() && foreignIdField.equals(joinedQueryBuilder.tableInfo.getIdField())) {
                joinInfo.localField = fieldType;
                joinInfo.remoteField = foreignIdField;
                return;
            }
        }
        // if this other field is a foreign field and its foreign-id field is our id
        for (FieldType fieldType : joinedQueryBuilder.tableInfo.getFieldTypes()) {
            if (fieldType.isForeign() && fieldType.getForeignIdField().equals(idField)) {
                joinInfo.localField = idField;
                joinInfo.remoteField = fieldType;
                return;
            }
        }

上面说明如果你想使用QueryBuilder进行连接,你需要确保你在id字段上加入一个连接,所以这在我看来像orm-lite中的QueryBuilder.java的限制。如果你已经提出在Coupon.java中作为id的外键它本来有用。

快速解决方法应该是使用原始查询字符串来实现您的目标。

例如 -

final GenericRawResults<Coupon> results = couponDao.queryRaw("SELECT "
                + "product_table.product_id AS id, "
                + "product_table.store_id AS storeId "
                + " FROM coupon_table "
                + "JOIN product_table ON coupon_table.store_id = product_table.store_id "
                + "WHERE product_table.store_id = 1", new RawRowMapper<Coupon>() {

                    @Override
                    public Coupon mapRow(String[] columnNames, String[] resultColumns) throws SQLException {
                        final Integer productId = Integer.parseInt(resultColumns[0]);
                        final Integer storeId = Integer.parseInt(resultColumns[1]);

                        final Product product = new Product();
                        product.setId(productId);
                        product.setStoreId(storeId);

                        final Coupon coupon = new Coupon();
                        coupon.setProduct(product);
                        return coupon;
                    }

                });

            final Coupon coupon = results.getResults().get(0);
            final Product product = coupon.getProduct();
            System.out.println("Product id is " + product.getId() + " , Store id is " + product.getStoreId());