一对多映射中无法获取多的一方【急】

ppmmwozuiai 2015-10-10 05:41:28
目的:列出所有具有下级Router的Router
问题:下级Router的集合size=0

其中使用upper_router_id字段引用本表的id
建表:

drop table IF EXISTS t_data_router;
CREATE TABLE IF NOT EXISTS t_data_router (
id BIGINT unsigned primary key,
name varchar(128) not null unique,
upper_router_id BIGINT unsigned,
as_terminal bit(1) not null,
hostname varchar(256) not null,
ip_address varchar(256),
description varchar(256)
);

映射配置

@Entity
@Table(name = "t_data_router")
public class DataRouter {

private Long id;
private String name;
private String hostname;
private String ipAddress;
private Boolean terminal;
private DataRouter upperRouter;
private Set<DataRouter> lowerRouters = new HashSet<DataRouter>();
private String description;

@Id
@GeneratedValue(generator = "shortUid")
@GenericGenerator(name = "shortUid", strategy = "dataroute.base.generator.ShortUUIDIncrementGenerator")
@Column(name = "id", columnDefinition = "BIGINT")
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

@Column(name = "name")
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Column(name = "hostname")
public String getHostname() {
return hostname;
}

public void setHostname(String hostname) {
this.hostname = hostname;
}

@Column(name = "ip_address")
public String getIpAddress() {
return ipAddress;
}

public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}

@Column(name = "as_terminal", columnDefinition = "BOOLEAN")
public Boolean getTerminal() {
return terminal;
}

public void setTerminal(Boolean terminal) {
this.terminal = terminal;
}

@Column(name = "description")
public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

@ManyToOne
@JoinColumn(name = "upper_router_id")
public DataRouter getUpperRouter() {
return upperRouter;
}

public void setUpperRouter(DataRouter upperRouter) {
this.upperRouter = upperRouter;
}

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "upper_router_id")
public Set<DataRouter> getLowerRouters() {
return lowerRouters;
}

public void setLowerRouters(Set<DataRouter> lowerRouters) {
this.lowerRouters = lowerRouters;
}

}



DAO中的查询代码

@SuppressWarnings("unchecked")
public <T> List<T> listQ(Class<T> c, String key, Object value) {
Query q = getSession().createQuery("from " + c.getSimpleName() + " where " + key + " = " + value);
return (List<T>) q.list();
}


目前的情况:hibernate已将SQL生成,但是获取下级router的size=0
将SQL复制到数据库中可以正确执行

Hibernate:
select
datarouter0_.id as id1_1_,
datarouter0_.description as descript2_1_,
datarouter0_.hostname as hostname3_1_,
datarouter0_.ip_address as ip_addre4_1_,
datarouter0_.name as name5_1_,
datarouter0_.as_terminal as as_termi6_1_,
datarouter0_.upper_router_id as upper_ro7_1_
from
t_data_router datarouter0_
where
datarouter0_.as_terminal=0
Hibernate:
select
lowerroute0_.upper_router_id as upper_ro7_1_0_,
lowerroute0_.id as id1_1_0_,
lowerroute0_.id as id1_1_1_,
lowerroute0_.description as descript2_1_1_,
lowerroute0_.hostname as hostname3_1_1_,
lowerroute0_.ip_address as ip_addre4_1_1_,
lowerroute0_.name as name5_1_1_,
lowerroute0_.as_terminal as as_termi6_1_1_,
lowerroute0_.upper_router_id as upper_ro7_1_1_
from
t_data_router lowerroute0_
where
lowerroute0_.upper_router_id=?
=============================0
...全文
89 点赞 收藏 1
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
ppmmwozuiai 2015-10-10
PS:使用急加载
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-10-10 05:41
社区公告
暂无公告