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