Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
hibernate中的奇怪现象不符合标准_Hibernate_Hibernate Criteria - Fatal编程技术网

hibernate中的奇怪现象不符合标准

hibernate中的奇怪现象不符合标准,hibernate,hibernate-criteria,Hibernate,Hibernate Criteria,我有两个具有多对多关系的表,称为位置和内容 映射是收集的,因为insert和其他查询都可以正常工作 我用 Criteria c = sessionFactory.getCurrentSession().createCriteria(Content.class); c.add(Restrictions.isEmpty("position")); 获取数据集,这意味着所有内容都不是由任何位置获取的 它工作正常 然后,我补充说: Criteria c = sessionFactory.getCurr

我有两个具有多对多关系的表,称为位置和内容

映射是收集的,因为insert和其他查询都可以正常工作

我用

Criteria c = sessionFactory.getCurrentSession().createCriteria(Content.class);
c.add(Restrictions.isEmpty("position"));
获取数据集,这意味着所有内容都不是由任何位置获取的

它工作正常

然后,我补充说:

Criteria c = sessionFactory.getCurrentSession().createCriteria(Content.class);
c.add(Restrictions.isEmpty("position"));
c.createAlias("position", "p");
c.add(Restrictions.eq("p.id", 100));
这意味着获取所有内容不是由位置100执行的

我得到一张空报税单

有些人建议我使用:

c.createAlias("position", "p");
c.add(Restrictions.not(Restrictions.eq("p.id", 100)))
它工作正常,但结果如下:

SELECT * FROM `position_has_content` WHERE content_id not in (select content_id from `position_has_content` where position_id = 100)
虽然我需要这样的结果:

SELECT * FROM `content` WHERE `content`.id not in (select content_id from `position_has_content` where position_id = 100)
这是如此不同

我能知道我错过了什么吗

问题解决后,我会不断更新

这种情况下的表格如下所示:

position          Position_has_content       content
id                     position_id             id
                       content_id
因此,在多对多的情况下,它们已经在hibernate中正确映射

映射关系的阶段:

@ManyToMany(mappedBy = "content")
    private Set<Position> position;
-------------------------------------------------------------
@ManyToMany(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinTable(name = "position_has_content",  joinColumns = { 
            @JoinColumn(name = "position_id") }, 
            inverseJoinColumns = { @JoinColumn(name = "content_id") })
    @OrderBy("position")
    private Set<Content> content;
@ManyToMany(mappedBy=“content”)
私人设定职位;
-------------------------------------------------------------
@ManyToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
@JoinTable(name=“position\u has\u content”,joinColumns={
@JoinColumn(name=“position\u id”)},
inverseJoinColumns={@JoinColumn(name=“content\u id”)})
@订购人(“职位”)
私有集内容;

映射是正确的,因为所有其他操作(不带“not in”、插入、删除两边的普通急切查询)都可以正常工作。

请在数据库中发布完整的实体,也许还有一些示例条目,以更好地说明您如何尝试筛选结果。我希望有很多方法来实现你期待的结果,但是你可以考虑使用一个分离的标准和存在/不存在的子句。不管怎样,一旦有更多的信息,我会仔细看看

当然,这里是细节。我使用了,但子查询不支持“不在”的好方法,或者我只是不知道如何实现它。非常感谢。@CharlesLi-如果您使用的是基于XML的配置,请发布完整的实体和hibernate映射文件。你能再检查一下你的关系是否正确吗。我能理解“内容”为什么会有一个位置,但我不理解它如何成为一种多对多关系。内容的一部分可以有多个位置吗?一个职位可以被多个内容占用吗?感谢您的帮助,因为职位维护一个内容列表,该列表将分析使用、使用或等待使用的内容的数量和种类。条件c=sessionFactory.getCurrentSession().createCriteria(Content.class);c、 createAlias(“位置”、“p”);c、 添加(限制条件,等式(“p.id”,100));List nc=c.List();List idList=new ArrayList();对于(Content cont:nc){idList.add(cont.getId());}Criteria c2=sessionFactory.getCurrentSession().createCriteria(Content.class).setFetchMode(“position”,FetchMode.SELECT);如果(nc.size()!=0)c2.add(Restrictions.not(Restrictions.in(“id”,idList)));返回c2.list();我知道这是没有效率和效果的,所以有什么想法可以帮助吗?非常感谢。