2 bixiaoshuan bixiaoshuan 于 2014.07.23 20:24 提问

跪求高手指导hibernate此hql 怎么写

hql = "select distinct u from User " 跪求高手指导此hql 怎么写
用hql查询User 的时候, 只(ˇ?ˇ) 想~查出User 以及User .Station.id、User .Station.name、.User Station.dept,以及User Station.dept.id、User Station.dept.name 因为不这样的话 执行效率太低了。页面刷新很慢%>_<%
[code=java]
public class User implements Serializable{

private static final long serialVersionUID = 7259027197507200040L;
private String id;
private String username;
private String password;
private int status;
private UserInfo userInfo;
private Set<Station> stations;

private Set<Role> roles;

public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public UserInfo getUserInfo() {
    return userInfo;
}
public void setUserInfo(UserInfo userInfo) {
    this.userInfo = userInfo;
}

public Set<Station> getStations() {
    return stations;
}
public void setStations(Set<Station> stations) {
    this.stations = stations;
}

public Set<Role> getRoles() {
    return roles;
}
public void setRoles(Set<Role> roles) {
    this.roles = roles;
}
public int getStatus() {
    return status;
}
public void setStatus(int status) {
    this.status = status;
}

[/code]
[code=java]
public class Station implements Serializable{

private static final long serialVersionUID = 1L;
private String id;
private String name;
private String descContent;
private Date createTime;
private int status;
private Dept dept;

private Set<User> users;

private Set<DimTag> dimTags;

public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getDescContent() {
    return descContent;
}
public void setDescContent(String descContent) {
    this.descContent = descContent;
}
public Date getCreateTime() {
    return createTime;
}
public void setCreateTime(Date createTime) {
    this.createTime = createTime;
}
public Dept getDept() {
    return dept;
}
public void setDept(Dept dept) {
    this.dept = dept;
}
public Set<User> getUsers() {
    return users;
}
public void setUsers(Set<User> users) {
    this.users = users;
}
public Set<DimTag> getDimTags() {
    return dimTags;
}
public void setDimTags(Set<DimTag> dimTags) {
    this.dimTags = dimTags;
}
public int getStatus() {
    return status;
}
public void setStatus(int status) {
    this.status = status;
}

}

[/code]
[code=java]
public class Dept implements Serializable{

private static final long serialVersionUID = 1L;
private String id;
private String name;

private Dept parent;
private String bh;
private String descContent;
private int status;
private Set<Station> station;

public Set<Station> getStation() {
    return station;
}
public void setStation(Set<Station> station) {
    this.station = station;
}
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Dept getParent() {
    return parent;
}
public void setParent(Dept parent) {
    this.parent = parent;
}
public String getBh() {
    return bh;
}
public void setBh(String bh) {
    this.bh = bh;
}
public String getDescContent() {
    return descContent;
}
public void setDescContent(String descContent) {
    this.descContent = descContent;
}
public int getStatus() {
    return status;
}
public void setStatus(int status) {
    this.status = status;
}

}

[/code]
跪求高手指导hibernate此hql 怎么写

1个回答

qwuack
qwuack   2014.07.23 23:21

可以返回station再拿出来处理,select distinct u.stations from User u
楼主似乎并没有设置懒加载,如@ManyToOne(fetch = FetchType.LAZY)
请楼主恶补下

qwuack
qwuack 我去。。查出来后用List<Map>接收不就好了。。。。。。。
大约 3 年之前 回复
bixiaoshuan
bixiaoshuan 你这样查出来的结果结构变了 用这种 select new Table(name,sex) from Table where nid= "要是查出来的结果 还是user里面 stations 这个结果 该怎么弄
大约 3 年之前 回复
qwuack
qwuack select distinct s.id,s.name,d.id,d.name from User u left join u.stations s left join s.dept d
大约 3 年之前 回复
bixiaoshuan
bixiaoshuan 我要查用户User 但是查出来的User.stations 只需要查出User.stations.id,User.stations.name
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片