hibernate 一对多查询问题

lycgxy 2010-05-11 10:08:04
想把多的一端通过最新的日期来限制唯一性,但是不行,代码如下:
StringBuffer hql=new StringBuffer();
hql.append("select i from Item i left outer join i.prices p ");
hql.append(" left outer join i.costs c ");
hql.append(" left outer join i.discounts d");
hql.append(" left outer join i.inventories inv");
hql.append(" where p.updateDate in (select max(pr.updateDate) from Price pr group by pr.itemId)");
hql.append(" and c.updateDate in (select max(co.updateDate) from Cost co group by co.itemId)");
hql.append(" and d.updateDate in (select max(dis.updateDate) from Discount dis group by dis.itemId)");
hql.append(" and inv.updateDate in (select max(inve.updateDate) from Inventory inve group by inve.itemId)");
hql.append(" and i.id = "+itemId);
List<Item> itemList = this.query(hql.toString());
查询出来之后,多的这端的集合还是有很多记录,hibernate实体如下
@Entity
@Table(name = "item", catalog = "sms")
public class Item implements java.io.Serializable {
private Integer id;

private Category category;

private String code;

private String name;

private Date updateDate;

private Date createDate;

private int quantity;

//private Set<Sale> sales = new HashSet<Sale>(0);

private Set<Discount> discounts = new HashSet<Discount>(0);

private Set<Cost> costs = new HashSet<Cost>(0);

private Set<Price> prices = new HashSet<Price>(0);

//private Set<ItemDetailInfo> itemDetailInfos = new HashSet<ItemDetailInfo>(0);

private Set<Inventory> inventories = new HashSet<Inventory>(0);

public Item() {
}

.............................................
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}

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

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "category_id")
public Category getCategory() {
return this.category;
}

public void setCategory(Category category) {
this.category = category;
}

@Column(name = "code", nullable = false, length = 13)
public String getCode() {
return this.code;
}

public void setCode(String code) {
this.code = code;
}

@Column(name = "name", nullable = false, length = 20)
public String getName() {
return this.name;
}

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

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "update_date", nullable = false, length = 19)
public Date getUpdateDate() {
return this.updateDate;
}

public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "create_date", nullable = false, length = 19)
public Date getCreateDate() {
return this.createDate;
}

public void setCreateDate(Date createDate) {
this.createDate = createDate;
}

@Column(name = "quantity", nullable = false)
public int getQuantity() {
return this.quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

/*@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "item")
public Set<Sale> getSales() {
return this.sales;
}

public void setSales(Set<Sale> sales) {
this.sales = sales;
}*/

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "itemId")
public Set<Discount> getDiscounts() {
return this.discounts;
}

public void setDiscounts(Set<Discount> discounts) {
this.discounts = discounts;
}

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "itemId")
public Set<Cost> getCosts() {
return this.costs;
}

public void setCosts(Set<Cost> costs) {
this.costs = costs;
}

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "itemId")
public Set<Price> getPrices() {
return this.prices;
}

public void setPrices(Set<Price> prices) {
this.prices = prices;
}

/*@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "item")
public Set<ItemDetailInfo> getItemDetailInfos() {
return this.itemDetailInfos;
}

public void setItemDetailInfos(Set<ItemDetailInfo> itemDetailInfos) {
this.itemDetailInfos = itemDetailInfos;
}*/

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "itemId")
public Set<Inventory> getInventories() {
return this.inventories;
}

public void setInventories(Set<Inventory> inventories) {
this.inventories = inventories;
}
}
...全文
106 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lycgxy 2010-05-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 z280298909 的回复:]
cascade = CascadeType.ALL 有这个配置 他就会把多的一端 关联所有的查出来!
[/Quote]
谢谢帮助,测试了一下,不能达到目的
醉解风情 2010-05-12
  • 打赏
  • 举报
回复
cascade = CascadeType.ALL 有这个配置 他就会把多的一端 关联所有的查出来!
醉解风情 2010-05-12
  • 打赏
  • 举报
回复
jpa 注解里!@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "itemId")
就可以实现级联删除 查询 更新
lycgxy 2010-05-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tangyu477 的回复:]
hibernate不支持联合查询吧
[/Quote]
它这么强大,不会吧!
tfish2014 2010-05-11
  • 打赏
  • 举报
回复
hibernate不支持联合查询吧

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧