Java 如何在休眠中编写连接查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/949996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to write join query in hibernate
提问by Jugal
I have created two beans User and VirtualDomain with many to many relationship
我创建了两个具有多对多关系的 bean User 和 VirtualDomain
@Entity
@Table(name = "tblUser")
public class User implements Serializable {
private Long id;
private String username;
private Set<VirtualDomain> virtualdomainset;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "username", length = 50, nullable = false)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@ManyToMany(targetEntity = VirtualDomain.class, cascade = {CascadeType.PERSIST},fetch=FetchType.EAGER)
@JoinTable(name = "tblUserDomainRel", joinColumns = @JoinColumn(name = "userid"), inverseJoinColumns = @JoinColumn(name = "domainid"))
public Set<VirtualDomain> getVirtualdomainset() {
return virtualdomainset;
}
public void setVirtualdomainset(Set<VirtualDomain> virtualdomainset) {
this.virtualdomainset = virtualdomainset;
}
}
@Entity
@Table(name = "tblVirtualDomain")
public class VirtualDomain {
private Long id;
private String domainname;
private Set<User> userset;
@Id
@JoinColumn(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "domain_name")
public String getDomainname() {
return domainname;
}
public void setDomainname(String domainname) {
this.domainname = domainname;
}
@ManyToMany(cascade = {CascadeType.ALL},fetch=FetchType.EAGER, mappedBy = "virtualdomainset", targetEntity = User.class)
public Set<User> getUserset() {
return userset;
}
public void setUserset(Set<User> userset) {
this.userset = userset;
}
}
how to get data of user like username related to particular domain through hibernate.
如何通过休眠获取与特定域相关的用户名等用户数据。
采纳答案by Blake Pettersson
To add to gid's answer, if for some reason you need to eagerly fetch an entites relations, then the join syntax would be join fetch.
要添加到 gid 的答案中,如果由于某种原因您需要急切地获取实体关系,那么连接语法将是 join fetch。
from VirtualDomain vd join fetch vd.usersset u
where vd.domainname = 'example.com' and u.username like 'foo%'
回答by Gareth Davis
Always difficult to write HQL without a test system...but here we go:
没有测试系统总是很难编写 HQL……但是我们开始了:
select u from VirtualDomain vd join User vd.usersset u
where vd.domainname = 'example.com' and u.username like 'foo%'
Let me know how you get on.
让我知道你是怎么办的。
One tip I often did prior to buying Intellji was to stop the app in the debugger and then use the immediate window to experiment with HQL.
在购买 Intellji 之前,我经常做的一个提示是在调试器中停止应用程序,然后使用即时窗口来试验 HQL。
The hibernate documentationon joins has always been a bit cryptic in my opinion.
在我看来,关于连接的休眠文档一直有点神秘。