Good afternoon. Learn Hibernate, it became necessary to write the following query using the criteria. There are two entities - a fruit shop and the type of communication - many to many. Accordingly, we have literally three tables (and the corresponding classes of Java): Fruit (id, name), Shop (id, name) and ShopFruit (shop_id, fruit_id). So, as with the criterion to obtain a list of stores selling, say, a tangerine? Thanks in advance.
下午好。学习Hibernate,有必要使用条件编写以下查询。有两个实体 - 一个水果店和通信类型 - 许多人。因此,我们实际上有三个表(以及相应的Java类):Fruit(id,name),Shop(id,name)和ShopFruit(shop_id,fruit_id)。那么,正如获得销售商店列表的标准一样,比如橘子?提前致谢。
1 个解决方案
#1
0
I think you can use an alias like so:
我想你可以使用这样的别名:
List questions = session.createCriteria(Shop.class)
.createAlias("Fruits", "f")
.add(Restrictions.eq("f.name", "tangerine"))
.list();
See also this question. However, I think you can also solve this with a set in your Hibernate mapping for Fruit:
另见这个问题。但是,我认为你也可以用你的Hibernate映射中的一个集合解决这个问题:
<set name="stores" table="ShopFruit" cascade="all">
<key column="fruit_id" />
<many-to-many column="store_id" />
</set>
Extends your Fruit class with a stores set:
使用商店集扩展您的Fruit类:
class Fruit {
...
Set<Store> stores;
...
}
If you than load the "tangerine" Fruit object from the database, the stores
variable should have all the stores selling it.
如果您要从数据库加载“tangerine”Fruit对象,那么stores变量应该包含销售它的所有商店。
#1
0
I think you can use an alias like so:
我想你可以使用这样的别名:
List questions = session.createCriteria(Shop.class)
.createAlias("Fruits", "f")
.add(Restrictions.eq("f.name", "tangerine"))
.list();
See also this question. However, I think you can also solve this with a set in your Hibernate mapping for Fruit:
另见这个问题。但是,我认为你也可以用你的Hibernate映射中的一个集合解决这个问题:
<set name="stores" table="ShopFruit" cascade="all">
<key column="fruit_id" />
<many-to-many column="store_id" />
</set>
Extends your Fruit class with a stores set:
使用商店集扩展您的Fruit类:
class Fruit {
...
Set<Store> stores;
...
}
If you than load the "tangerine" Fruit object from the database, the stores
variable should have all the stores selling it.
如果您要从数据库加载“tangerine”Fruit对象,那么stores变量应该包含销售它的所有商店。