简体   繁体   English

休眠条件列表必须包含另一个列表

[英]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. 实际上,您不需要执行任何其他操作,而是告诉Hibernate始终随您的实体一起加载它。

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 . 因此, OneToMany注释告诉Hibernate在链接到您的表的Tag表中查找标签,并始终执行此操作: FetchType.EAGER

Actually Hibernate will create two tables with a foreign key in the Tag table. 实际上,Hibernate将在Tag表中创建两个带有外键的表。

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. 如果您不想延迟加载此列表,可以设置FetchType.LAZY ,然后需要做一些额外的工作才能填充此列表。

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? 通过标签过滤访问列表可以看到以下答案: 如何在Hibernate下加入两个不同的条件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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