我需要在同一个表上执行内连接。下面是我目前正在执行的原始查询。
String tagValueRowsSQL = "SELECT itv1.ItemTagValueID,itv1.FkItemID," +
"itv1.FkTagID,itv1.TagIndex,itv1.ValueIndex,itv1.Value,itv1.LastUpdateDateTime" +
" FROM tag.ItemTagValue itv1" +
" INNER JOIN tag.ItemTagValue itv2" +
" on itv1.fkTagID=" + tagIDsAndValues.get(LOCATION_STORE_CONFIG_TAG_NAME).getTagID() +
" and itv1.Value=" + storeId +
" and itv2.fkTagID = " + tagIDsAndValues.get(SENSOR_TYPE_TAG_NAME).getTagID() +
" and itv2.Value=" + sensorType;
RawSql rawSqlForSensorsInStoreAndOfType = RawSqlBuilder.parse(tagValueRowsSQL)
.columnMapping("itv1.ItemTagValueID", "ItemTagValueID")
.columnMapping("itv1.FkItemID", "FkItemID")
.columnMapping("itv1.FkTagID", "FkTagID")
.columnMapping("itv1.TagIndex", "TagIndex")
.columnMapping("itv1.ValueIndex", "ValueIndex")
.columnMapping("itv1.Value", "Value")
.columnMapping("itv1.LastUpdateDateTime", "LastUpdateDateTime")
.create();
Query<ItemTagValueModel> query = Ebean.find(ItemTagValueModel.class);
List<ItemTagValueModel> sensorsTagValueData = query.setRawSql(rawSqlForSensorsInStoreAndOfType).findList();
使用RawSQL执行上述操作似乎非常笨拙。如何在不使用RawSQL的情况下执行上述操作?我查看了这个问题Ebeans where columnA equals columnB in same table,但我不确定如何将这个答案应用于我的情况。任何帮助将非常感激。