2
例如,我有这样的查询与许多一对多WHERE子句
<class name="my.test.model.Product" table="PRODUCT">
...
<set name="retailers" table="product_shop" lazy="false">
<key column="PRODUCT_ID" />
<many-to-many column="SHOP_ID" class="my.test.model.Shop" />
</set>
...
</class>
映射文件现在我想查询某家商店A的产品中集(连接表)想到这样的事情:
String searchHql = "select p from Product p inner join p.retailers retailing where p.retailers.shop_id = :shopId";
@SuppressWarnings("unchecked")
List<Product> productList = sessionFactory.getCurrentSession().createQuery(searchHql).setInteger("shopId", shopId).list();
但它不会工作。返回的错误是:
无法解析属性:shop_id:my.test.model.Shop。我搜索了很多,但仍然没有找到正确的方法来访问hql中的“多对多”子集。这可能吗?或者我需要将Product_Shop表映射到模型类?
UPDATE:因为看起来没有其他办法,我最终将Product_Shop映射到类中。
不幸的是,shop_id是列名。我的“Shop”模型的ID为“id”(与Product相同),所以当我使用“retailing.id”时,它使用“Product”的“id”代替。 – 2012-02-29 07:24:27
HQL查询从不使用表名和列名,但始终使用实体名和字段/属性名。 – 2012-02-29 08:01:42
是的,但是这次连接表中的2个属性名称之间存在重复。 – 2012-02-29 09:09:23