简体   繁体   English

Hibernate Criteria API where子句在多对多关系中

[英]Hibernate Criteria API where clause in many to many relation

I have relation many to many with extra fileds in linking table(mapping in xml files). 我与链接表中的额外文件(在xml文件中映射)有很多对很多的关系。 Using criteria api how to add restrictions to name of product? 使用条件API如何为产品名称添加限制?

public class Recipe implements Serializable{

    private int id_re;

    private String name;

    private Set<ProductRecipe> listOfRecipe_Product = new HashSet<>(0);
}


public class ProductRecipe implements Serializable{

    private ProductRecipeMapping id;

    private float quantity;
}

public class ProductRecipeMapping implements Serializable{

    private Product product;

    private Recipe recipe;
}

public class Product implements Serializable{

    private int id_p;

    private String name;
}

Mapping: 制图:

<class entity-name="recipe" name="Recipe" table="recipe">
        <id name="id_re" type="java.lang.Integer">
            <column name="id_re" />
            <generator class="identity" />
        </id>
        <set name="listOfRecipe_Product" table="recipe_product" lazy="false" fetch="select" cascade="all">
            <key>
                <column name="id_re" not-null="true" />
            </key>

            <one-to-many entity-name="productRecipe" />
        </set>
</class>

<class entity-name="productRecipe" name="ProductRecipe" table="recipe_product">
        <composite-id name="id" class="ProductRecipeMapping" >
            <key-many-to-one name="recipe" entity-name="recipe" column="id_re" />
            <key-many-to-one name="product" entity-name="product" column="id_p" />
        </composite-id>

        <property name="quantity" type="float" column="quantity" />

</class>    

<class entity-name="product" name="Product" table="product">

        <id name="id_p" type="java.lang.Integer" column="id_p">
            <generator class="identity" />
        </id>

        <property name="name" column="name" type="java.lang.String" not-null="true" length="255"/>

</class>

EG I use criteria for get recipe with name test: 例如,我使用条件来获取名称测试的配方:

Criteria cr = session.createCriteria(Recipe.class);
cr.add(Restrictions.eq("name", "test")); 

but I don't know to get all recipes with list name of products something like cr.add(Restrictions.eq("product.name", "test")); 但是我不知道所有带有产品列表名称的食谱都像cr.add(Restrictions.eq(“ product.name”,“ test”)))一样; (but not work) (但不起作用)

I use 2 idea to resolve this problem but nothing work: 我使用2个想法来解决此问题,但没有任何效果:

1) Restrictions.eq("listOfRecipe_Product.id.product.name", "test") but i get error org.hibernate.QueryException: could not resolve property: listOfRecipe_Product.id.product.name of: recipe 1) Restrictions.eq("listOfRecipe_Product.id.product.name", "test")但我收到错误org.hibernate.QueryException: could not resolve property: listOfRecipe_Product.id.product.name of: recipe

2) 2)

cr.createCriteria("listOfRecipe_Product")
    .createCriteria("id")
    .createCriteria("product")
    .add(Restrictions.eq("name", "test"));

I get error org.hibernate.QueryException: Criteria objects cannot be created directly on components. Create a criteria on owning entity and use a dotted property to access component property: listOfRecipe_Product.id 我收到错误org.hibernate.QueryException: Criteria objects cannot be created directly on components. Create a criteria on owning entity and use a dotted property to access component property: listOfRecipe_Product.id org.hibernate.QueryException: Criteria objects cannot be created directly on components. Create a criteria on owning entity and use a dotted property to access component property: listOfRecipe_Product.id

You will need to create aliases in order to add constraints to mapped entities; 您将需要创建别名,以便向映射的实体添加约束。

eg: 例如:

Criteria cr = session.createCriteria(Recipe.class)
  .createAlias("listOfRecipe_Product", "listOfRecipe_Product")
  .createAlias("listOfRecipe_Product.id", "id")
  .createAlias("id.product", "product")
  .add(Restrictions.eq("product.name", "test"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM