У меня есть отношение многих ко многим с дополнительными именами в таблице ссылок (сопоставление в xml-файлах). Используя критерии 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;
}
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>
Е.Г. Я использую критерии Получить рецепт с испытанием имени:
Criteria cr = session.createCriteria(Recipe.class);
cr.add(Restrictions.eq("name", "test"));
, но я не знаю, чтобы получить все рецепты со списком именем продуктов что-то вроде cr.add (Restrictions.eq («product.name», " контрольная работа")); (Но не работают)
Я использую 2 идеи, чтобы решить эту проблему, но ничего не работа:
1) Restrictions.eq("listOfRecipe_Product.id.product.name", "test")
, но я получаю ошибку org.hibernate.QueryException: could not resolve property: listOfRecipe_Product.id.product.name of: recipe
2)
cr.createCriteria("listOfRecipe_Product")
.createCriteria("id")
.createCriteria("product")
.add(Restrictions.eq("name", "test"));
Я получаю ошибку 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
Пожалуйста, более конкретно: Вы хотите, чтобы написать запрос, который будет возвращать Alls Рецепт, которая использует продукт с определенным именем? Также: Что вы пробовали до сих пор? –
Я добавил пример в основное описание –
Я пытаюсь использовать Restrictions.eq ("listOfRecipe_Product.id.product.name", "test"), но я получаю ошибку org.hibernate.QueryException: не удалось разрешить свойство: listOfRecipe_Product.id.product. name: recipe –