Hibernate QBE 级联查询

首先,有两个类Trade和Client,它们是多对一的关系。

Trade类:

private Client client ; //客户 @ManyToOne //@JoinColumn(name="CLIENT_ID") public Client getClient() { return client; } 

Client类:

private Set<Trade> trades; //与Trade一对多 @OneToMany(mappedBy="client", cascade={CascadeType.REMOVE} //fetch=FetchType.EAGER ) public Set<Trade> getTrades() { return trades; } public void setTrades(Set<Trade> trades) { this.trades = trades; } 

 

当通过Criteria查询某个Client_ID对应的所有trade时,就涉及到级联查询,使用方法如下:

 

session = hibernateTemplate.getSessionFactory().getCurrentSession(); Criteria criteria = session.createCriteria(Trade.class); Example example = Example.create(trade); example.enableLike(MatchMode.ANYWHERE);//使QBE支持模糊查询 criteria.add(example); if(null != trade.getClient()) { //对trade的client属性增加查询条件 criteria.createCriteria("client").add(Example.create(trade.getClient())); } 

 

Hibernate打印的sql语句类似于:

select * from Trade t inner join Client c on t.client_id = c.client_id where (1=1) and (c.xx = x);

 

你可能感兴趣的:(JOIN,sql,c,Hibernate,session,null)