假设我们有以下实体: 项目:
class Item {
...
@Index(unique=true)
private String guid;
...
@ToMany
@JoinEntity(entity = JoinItemsWithTags.class, sourceProperty = "itemGuid", targetProperty = "tagName")
private List<Tag> tagsWithThisItem;
...
}
标签:
class Tag {
@Id
private Long localId;
@Index(unique = true)
private String name;
...
}
我们需要加入他们。这是我的加入实体类:
@Entity(nameInDb = "item_tag_relations")
class JoinItemsWithTags {
@Id
private Long id;
private String itemGuid;
private String tagName;
...
}
我想使用标签名称作为连接属性而不是 Long id,因为在与服务器同步时更容易支持一致性。但是目前 Item 类中的标签 getter 总是返回一个空列表。我查看了日志,发现生成的查询在该 getter 内部使用:
SELECT * <<-- there were a long sequence of fields
FROM "tags" T JOIN item_tag_relations J1
ON T."_id"=J1."TAG_NAME" <<-- here is the problem, must be `T."NAME"=J1."TAG_NAME"`
WHERE J1."ITEM_GUID"=?
所以问题是连接是基于标签的_id
字段的。GeneratedList<Tag> _queryItem_TagsWithThisItem(String itemGuid)
方法隐式使用该 id 进行连接:
// this `join` nethod is overloaded and pass tag's id as source property
queryBuilder.join(JoinItemsWithTags.class, JoinItemsWithTagsDao.Properties.TagName)
.where(JoinItemsWithTagsDao.Properties.ItemGuid.eq(itemGuid));
正确的方法是这种情况可能如下,我想:
// source property is passed explicitly
queryBuilder.join(/* Desired first parameter -->> */ TagDao.Properties.Name,
JoinItemsWithTags.class, JoinItemsWithTagsDao.Properties.TagName)
.where(JoinItemsWithTagsDao.Properties.ItemGuid.eq(itemGuid));
但是这段代码在生成的 dao 中,我不知道如何处理它。有没有办法解决这个问题?