简体   繁体   中英

Hibernate criteria list must contains another list

Given the simplified model:

public class Access {
  private Set<Tag> tags;
}

public class Item {
  private Set<Tag> tags;
}

An access grant the access on every items that fully contains his tags, no problem if the item has more tags or not.

But I have no idea how to create an hibernate criteria query for this. Could you help me?

You actually don't need to do anything other then tell Hibernate to load it always with your entity.

So option one, load always:

public class Access {

        @OneToMany(cascade = {CascadeType.ALL}, orphanRemoval = true, targetEntity = Tag.class, fetch = FetchType.EAGER)
        @JoinColumn(nullable = true)
        protected Set<Tag> tags;
    }

So the OneToMany annotation is telling Hibernate to look for tags in the Tag table linked to your table and to do this always: FetchType.EAGER .

Actually Hibernate will create two tables with a foreign key in the Tag table.

In case you wan't to lazy load this list you can set FetchType.LAZY , then you need to do some additional work in order to fill this list.

A simple load will fill up all values as stored:

session.get(Access.class, key);

Filtering Access list by Tags see this answer: How to join two different criterias under Hibernate?

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