Association sleep request

How can I execute the following Hibernate request using the criteria API. I have a Parent object with a list. I would like to find all parents and find which parents contain the specified child. those.List<Parent> findParents(Child child);

Thank.

+3
source share
2 answers

It seems to work for me. The product is the parent, and the Ingredient is the Child. He, we hope, will find all products containing this ingredient. I could not fully verify this.

public IList<Product> GetProductsWithIngredient(Ingredient ingredient)
{
    using (ITransaction transaction = session.BeginTransaction())
    {
        ICriteria criteria = session.CreateCriteria<Product>();
        criteria.CreateCriteria("Ingredients")
        .Add(Restrictions.Eq("GUID", ingredient.GUID));

        return criteria.List<Product>();
    }
}

Hope this helps :)

NOTE. GUID is my unique identifier.

EDIT: , , . zoidbeck.

+2

Java :

Criteria criteria = session.createCriteria(Parent.class,"parent")
    .createAlias("child","child")
    .add(Restriction.eq("child.name",child.getName());

   List<Parent> parents = criteria.list();

.

0

Source: https://habr.com/ru/post/1736677/


All Articles