我们假设我们有以下实体: 项:
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总是返回一个空列表。我查看了log并找到了在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
字段。生成的List<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中,我不知道如何用它做任何事情。有没有办法解决这个问题?