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
Java 休眠多对多集合筛选_Java_Hibernate_Filter_Many To Many - Fatal编程技术网

Java 休眠多对多集合筛选

Java 休眠多对多集合筛选,java,hibernate,filter,many-to-many,Java,Hibernate,Filter,Many To Many,我有以下POJO,里面有一套: class Word { private Long id; private String word; private int type = WordListFactory.TYPE_DEFAULT; private Set<Word> refs = new HashSet<Word>(); ... } 在获取数据的代码中,我启用了过滤器并设置了参数。根据日志,hibernate似乎忽略了这个特定的过滤器,

我有以下POJO,里面有一套:

class Word {
    private Long id;
    private String word;
    private int type = WordListFactory.TYPE_DEFAULT;

    private Set<Word> refs = new HashSet<Word>();
...
}
在获取数据的代码中,我启用了过滤器并设置了参数。根据日志,hibernate似乎忽略了这个特定的过滤器,因为它在生成的SQL查询中没有任何条件

  • 在条件中使用附加条件

      Word result = null;
    
    session.beginTransaction();
    
    Criteria crit = session.createCriteria(Word.class);       
    
    crit.add(Restrictions.like("word", key))
         .createAlias("refs", "r")
         .add(Restrictions.eq("r.type", getType()));//added alias and restriction for type
    
     List list = crit.list();
    
     if(!list.isEmpty())
         result = list.get(0);   
    
     session.getTransaction().commit();
现在,生成的SQL似乎还可以

   select
        this_.id as id0_1_,
        this_.word as word0_1_,
        this_.type as type0_1_,
        refs3_.word_id as word1_,
        r1_.id as word2_,
        r1_.id as id0_0_,
        r1_.word as word0_0_,
        r1_.type as type0_0_ 
    from
        word this_ 
    inner join
        word_key refs3_ 
            on this_.id=refs3_.word_id 
    inner join
        word r1_ 
            on refs3_.word_ref_id=r1_.id 
    where
        this_.word like ? 
        and r1_.type=?
但是在这个查询之后,有另一个查询获取所有项目

 select
        refs0_.word_id as word1_1_,
        refs0_.word_ref_id as word2_1_,
        word1_.id as id0_0_,
        word1_.word as word0_0_,
        word1_.type as type0_0_ 
    from
        word_key refs0_ 
    left outer join
        word word1_ 
            on refs0_.word_ref_id=word1_.id 
    where
        refs0_.word_id=?

也许我做错了什么?

从您给定的代码片段中可以看出以下几点:

  • 在多对多关系的情况下,需要3个表、两个实体表和一个联接表。但由于您使用的是相同的实体字,我认为给定的表结构和映射似乎很好
  • 尝试使用HQL并指定“LEFT JOIN FETCH”来指定需要在初始sql SELECT中检索哪些关联
查看此链接相关的多对多关系,但他们使用了查询条件


So HQL-join-like session.createQuery(“从Word作为w-join获取w.refs作为r,其中w.Word-like:ww和r.type=:tt”).setString(“ww”,key).setInteger(“tt”,getType()).uniqueResult();真的很有帮助。限制和过滤器为什么不能正确工作,这仍然是个谜。不管怎样,谢谢你的提示。
   select
        this_.id as id0_1_,
        this_.word as word0_1_,
        this_.type as type0_1_,
        refs3_.word_id as word1_,
        r1_.id as word2_,
        r1_.id as id0_0_,
        r1_.word as word0_0_,
        r1_.type as type0_0_ 
    from
        word this_ 
    inner join
        word_key refs3_ 
            on this_.id=refs3_.word_id 
    inner join
        word r1_ 
            on refs3_.word_ref_id=r1_.id 
    where
        this_.word like ? 
        and r1_.type=?
 select
        refs0_.word_id as word1_1_,
        refs0_.word_ref_id as word2_1_,
        word1_.id as id0_0_,
        word1_.word as word0_0_,
        word1_.type as type0_0_ 
    from
        word_key refs0_ 
    left outer join
        word word1_ 
            on refs0_.word_ref_id=word1_.id 
    where
        refs0_.word_id=?