我有这些实体:
ER Diagram http://i64.tinypic.com/24b6sut.png
假设我收到此map<String,String>
:
&#34;性别&#34;:&#34;男性&#34;
&#34;年龄&#34;:&#34; 30&#34;
我需要找到包含 TargetGroup的广告系列,其中包含地图中的所有这些键/值。 例如:
Map<String,String> keyValueParameters = new HashMap<String,String>();
keyValueParameters.put("gender","male");
keyValueParameters.put("age","30");
List<Campaign> campaignsResult = getCampaigns(keyValueParameters);
Campaign aCampaign = campaignsResult.get(someIndex);
TargetGroup aTargetGroup = aCampaign.getTargetGroup();
List<TargetValue> targetValues = aTargetGroup.getTargetValues();
targetValues.get(x).getKey() //"gender"
targetValues.get(x).getValue() //"male"
targetValues.get(y).getKey() //"age"
targetValues.get(y).getValue() //"30"
targetValues.get(z).getKey() //some other key
答案 0 :(得分:1)
查询返回具有键/值对的目标组列表。 (条件计数的HAVING部分中有2个
SELECT targetGroupID
FROM TargetGroup tg
INNER JOIN TargetValue tv ON tg.targetGourpId=tv.targetGourpId
WHERE (tv.key='gender' and tv.value='male')
OR (tv.key='age' and tv.value='30')
GROUP BY targetGroupID
HAVING COUNT(targetGroupID)>=2
然后我们可以使用上面的查询来获取适当的广告系列
SELECT *
FROM Campaign c
INNER JOIN (the select above) as sub ON c.targetGroupID=sub.targetGroupID
纯粹的冬眠几乎不可能。 HAVING可能是个问题。您可以尝试为此编写自己的hql。