私はリンクテーブル(xmlファイルのマッピング)に余分なfiledsと多くの関係を持っています。 criteria APIを使用すると、製品の名前に制限を追加する方法は?Hibernate Criteria 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;
}
マッピング:例えば
<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>
"、
Criteria cr = session.createCriteria(Recipe.class);
cr.add(Restrictions.eq("name", "test"));
が、私は製品 cr.addのようなもの(Restrictions.eq(「product.name」のリストの名前を持つすべてのレシピを取得するのか分からない:私は名前テストしてgetレシピの基準を使用しますテスト"));
cr.createCriteria("listOfRecipe_Product")
.createCriteria("id")
.createCriteria("product")
.add(Restrictions.eq("name", "test"));
私はエラーを取得する)
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アイデアを使用し
を(ただし機能しません) 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
より具体的にしてください:あなたは、特定の名前を持つ製品を使用オールズレシピを返しますクエリを書いてみませんか?また:あなたはこれまでに何を試しましたか? –
私は主な説明の例を追加しました –
Restrictions.eq( "listOfRecipe_Product.id.product.name"、 "test")を使用してみましたが、エラーが発生しますorg.hibernate.QueryException:プロパティーを解決できませんでした:listOfRecipe_Product.id.product。名前:レシピ –