2016-09-13 3 views
1

私はリンクテーブル(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

+0

より具体的にしてください:あなたは、特定の名前を持つ製品を使用オールズレシピを返しますクエリを書いてみませんか?また:あなたはこれまでに何を試しましたか? –

+0

私は主な説明の例を追加しました –

+0

Restrictions.eq( "listOfRecipe_Product.id.product.name"、 "test")を使用してみましたが、エラーが発生しますorg.hibernate.QueryException:プロパティーを解決できませんでした:listOfRecipe_Product.id.product。名前:レシピ –

答えて

0

マップされたエンティティに制約を追加するためにエイリアスを作成する必要がありますies;

例えば:

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")); 
+0

マッピングに何か問題がありますか?私はエラーorg.hibernate.QueryExceptionを取得します:コンポーネントのコンポーネントに直接作成することはできません。エンティティの所有に関する基準を作成し、点線のプロパティを使用してコンポーネントプロパティにアクセスします。listOfRecipe_Product.id –

+0

エンティティのマッピングを表すように例を変更しました。 – Marius

+0

org.hibernate.QueryException:レシピ:レシピ:レシピ:レシピ:レシピ すべてのメソッドが正しく動作しないことを知りません。アクセスについてのエラーが発生しました....時間と助けのためにthx –