本文介绍了如何检索特定关系中存在的所有记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一类电脑,每一个都可以组装。我需要检索那些有Assembler的Computer类的记录。 @Entity
公共类计算机{
...
@OneToMany
private Set< Assembler>装配;
....
}
@实体
公共类汇编程序{
...
@OneToOne
私有用户用户;
@OneToMany
@LazyCollection(LazyCollectionOption.FALSE)
private Set< Tools>工具;
....
}
@实体
公共类工具{
....
@OneToOne
私有电子电子;
@OneToOne
private手动手动;
$ b
代码
Criteria cre = session.createCriteria(Computer.class,computer)
.createAlias(computer.assembler,assem)
cre 。新增(Restrictions.not(Restrictions.eq( assem.id,NULL)));
List computer =(List< Computer>)cre.list();
System.out.println(computer.size()):<<返回0,但应返回1
解决方案
/ p>
cre.add(Restrictions.not(Restrictions.eq(assem.id,null)));
试试:
cre.add(Restrictions.isNotEmpty(assemblers));
(删除别名)
I have a class of computer and each of it can be assembled. I need to retrieve those records of Computer class that has Assembler. I've tried to use criteria but could not retrieve them.
@Entity
public class Computer{
....
@OneToMany
private Set <Assembler> assemblers;
....
}
@Entity
public class Assembler{
...
@OneToOne
private User user;
@OneToMany
@LazyCollection(LazyCollectionOption.FALSE)
private Set<Tools> tools;
....
}
@Entity
public class Tools{
....
@OneToOne
private Electronic electronic;
@OneToOne
private Manual manual;
}
Code
Criteria cre = session.createCriteria(Computer.class, "computer")
.createAlias("computer.assembler", "assem")
cre.add(Restrictions.not(Restrictions.eq("assem.id",null)));
List computer = (List<Computer>) cre.list();
System.out.println(computer.size()): << returns zero but should return 1
解决方案
Instead of using:
cre.add(Restrictions.not(Restrictions.eq("assem.id",null)));
Try:
cre.add(Restrictions.isNotEmpty("assemblers"));
(remove the alias)
这篇关于如何检索特定关系中存在的所有记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!