Joining classes in ORMLite for Android throws SQL exception: could not find a foreign class or vice versa

StackOverflow https://stackoverflow.com/questions/23222605

  •  07-07-2023
  •  | 
  •  

Вопрос

I'm trying to create a join query using QueryBuilder for two different classes, a Product class and a Coupon class, that references a Product attribute, the 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;
}

I need to get a Coupon list based on the Product 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();
}

This query is throwing a SQL Exception:

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)

The exception says my classes are not related by a foreign field, but the Coupon class has a foreign Product attribute, annotated properly. I already checked the database, the values in the tables are correct.

How can I fix it?

Это было полезно?

Решение

Looking at the code inside 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;
            }
        }

Above states that if you want to make a join using QueryBuilder, you need to make sure you put a join on id field so this seems to me like a limitation of QueryBuilder.java in orm-lite.If you have put up a foreign key as id in Coupon.java It would have worked.

A quick workaround should be to use a raw query string to achieve what you want.

For example -

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());
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top