[英]Hibernate do a projection of count alias
这是我的代码:
class A{
Integer id
String name
Set<B> bs;
//Getters and setters
}
class B{
Integer id;
A a;
//Getters and setters
}
我需要进行咨询以获取A的所有行,并且对于每一行都需要了解很多B对A的引用。
我有SQL查询,但是我不知道如何在Java中使用休眠
简化查询:
select
a.ID as y0_,
( select count(*) from
B b
where b.c47=a.ID
and b.FDL='N'
) as count,
from
A a
inner join
C c
on a.operario=c.ID
where
a.DID=2
and a.FDL='N'
提前致谢
@Entity
@Table(name = "a_table")
class A
{
@Id
@GeneratedValue
Integer id;
@Column
String name;
@OneToMany(mappedBy = "a")
Set<B> bs;
//Getters and setters
}
@Entity
@Table(name = "b_table")
class B
{
@Id
@GeneratedValue
Integer id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "a_id")
A a;
//Getters and setters
}
HQL:
select a.id, count(bs)
from A a
left join a.bs bs
inner join a.c c
where
a.id = 2
and a.FDL='N'
and bs.FDL='N'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.