简体   繁体   中英

JPA superset using CriteriaQuery API

Given the following classes:

@Entity
public class Recipe {

    @Id
    @Column
    private long id; 

    @ElementCollection
    @CollectionTable(name = "recipe_ingredient", joinColumns = @JoinColumn(name = "id"))
    private Set<String> ingredients;

    ...
}

How can I write the in JPA using the CriteriaQuery API the following: "Given a list of ingredients, return the Recipes that have at least all of the specified ingredients"

Offhand, it can be something like:

criteria.add(Restrictions.in("recipe.ingredients", ingredients));

where ingredients is a collection of ingredient strings. Add an alias if ingredients throws you an exception.

Warning: Criteria API is now deprecated .

Otherwise, you can take a look here for an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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