1.java 实体对象
package com.helloworld.domain;
// Generated 2012-3-7 18:14:29 by Hibernate Tools 3.2.1.GA
/**
* TbAccount generated by hbm2java
* @author liuqing
*/
public class TbAccount implements java.io.Serializable {
private Integer id;
private TbPerson tbPerson;
private String name;
public TbAccount() {
}
public TbAccount(TbPerson tbPerson, String name) {
this.tbPerson = tbPerson;
this.name = name;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public TbPerson getTbPerson() {
return this.tbPerson;
}
public void setTbPerson(TbPerson tbPerson) {
this.tbPerson = tbPerson;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
2.实体对象
package com.helloworld.domain;
// Generated 2012-3-7 18:14:29 by Hibernate Tools 3.2.1.GA
/**
* TbJws generated by hbm2java
* @author liuqing
*/
public class TbJws implements java.io.Serializable {
private Integer id;
private TbPerson tbPerson;
private String jwsname;
public TbJws() {
}
public TbJws(TbPerson tbPerson, String jwsname) {
this.tbPerson = tbPerson;
this.jwsname = jwsname;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public TbPerson getTbPerson() {
return this.tbPerson;
}
public void setTbPerson(TbPerson tbPerson) {
this.tbPerson = tbPerson;
}
public String getJwsname() {
return this.jwsname;
}
public void setJwsname(String jwsname) {
this.jwsname = jwsname;
}
}
package com.helloworld.domain;
// Generated 2012-3-7 18:14:29 by Hibernate Tools 3.2.1.GA
import java.util.HashSet;
import java.util.Set;
/**
* TbPerson generated by hbm2java
* @author liuqing
*/
public class TbPerson implements java.io.Serializable {
private Integer id;
private String name;
private Set tbJwses = new HashSet(0);
private Set tbAccounts = new HashSet(0);
public TbPerson() {
}
public TbPerson(String name, Set tbJwses, Set tbAccounts) {
this.name = name;
this.tbJwses = tbJwses;
this.tbAccounts = tbAccounts;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set getTbJwses() {
return this.tbJwses;
}
public void setTbJwses(Set tbJwses) {
this.tbJwses = tbJwses;
}
public Set getTbAccounts() {
return this.tbAccounts;
}
public void setTbAccounts(Set tbAccounts) {
this.tbAccounts = tbAccounts;
}
}
3. QBC 多表查询方法
public static void main(String args[]) {
Session session = new Configuration().configure().buildSessionFactory().openSession();
org.hibernate.Criteria criteria = session.createCriteria(TbPerson.class);
//对集合对象创建别名
criteria.createAlias("tbJwses", "a_jws");
criteria.add(org.hibernate.criterion.Restrictions.eq("a_jws.jwsname", "3"));
List list = criteria.list();
for (int i = 0 ; i < list.size(); i++) {
System.out.println("===" + ((TbPerson)list.get(i)).getName() + " id:" + ((TbPerson)list.get(i)).getId() );
}
}
4. HQL 语言的实现
List list = session.createQuery("SELECT p FROM TbPerson p inner join p.tbJwses j WHERE j.jwsname='3'").list();
for (int i = 0 ; i < list.size() ; i++) {
System.out.println(list.get(i).getClass());
}