【Hibernate】双向多对多Set查询
一个计划对于多个竞价,一个竞价对应多个计划。
1.实体
/** * @author Tidy * @Description 计划 */ public class EbgStockPlanContent extends Model { /***省略***/ private Set<EbgBidding> biddingSet = new HashSet<EbgBidding>(); /***省略getset***/ } /** * @author Tidy * @Description 竞价 */ public class EbgStockPlanContent extends Model { /***省略***/ private Set<EbgStockPlanContent> planSet = new HashSet<EbgStockPlanContent>(); /***省略getset***/ }
2.多对多配置(xml方式)
<set name="biddingSet" table="T_EB_BIDDING_PLAN_COMPOSE" cascade="none" inverse="false" lazy="true"> <key column="PLAN_CONTENT_ID"></key> <many-to-many class="gdgpes.ebg.model.bid.EbgBidding" column="BIDDING_ID"></many-to-many> </set> <set name="planSet" table="T_EB_BIDDING_PLAN_COMPOSE" cascade="none" inverse="false" lazy="true"> <key column="BIDDING_ID"></key> <many-to-many class="gdgpes.ebg.model.plan.EbgStockPlanContent" column="PLAN_CONTENT_ID"></many-to-many> </set>
3.测试
4.elements()操作set集合
//remark
hql.append(" and mo.id not in (").append("select elements(planSet) from EbgBidding t1 where t1.type = 1").append(") and mo.price=5 ");
//1.直接查询set记录
select elements(planSet) from EbgBidding t1 where t1.type = 1
//2.in set记录
and mo.id not in elements(mo.biddingSet)
上例1.对应生成的sql
select planset2_.PLAN_CONTENT_ID
from T_EB_BIDDING ebgbidding1_,
T_EB_BIDDING_PLAN_COMPOSE planset2_
where ebgbidding1_.ID = planset2_.BIDDING_ID
and ebgbidding1_.TYPE = 1