有两张表一张是主键表,一张是外键表:
主键表:CREATE TABLE `jz_business` (
  `bs_id` int(10) unsigned NOT NULL auto_increment,
  `bsname` varchar(150) NOT NULL,
  `comment` varchar(200) default NULL,
  PRIMARY KEY  (`bs_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;外键表:
CREATE TABLE `jz_gtype` (
  `gt_id` int(10) unsigned NOT NULL auto_increment,
  `gt_name` varchar(100) NOT NULL,
  `bs_id` int(11) unsigned default NULL,
   index (bs_id),
   FOREIGN KEY (bs_id) REFERENCES jz_business (bs_id) ON DELETE RESTRICT ON UPDATE CASCADE,
   PRIMARY KEY  (`gt_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;生成的Pojo类为:public abstract class JzBusiness implements java.io.Serializable {
private Integer bsId;
private String bsname;
private String comment;
private Set jzGtypes = new HashSet(0);
        public Integer getBsId() {
return this.bsId;
}
public void setBsId(Integer bsId) {
this.bsId = bsId;
}
public String getBsname() {
return this.bsname;
}
public void setBsname(String bsname) {
this.bsname = bsname;
}
public String getComment() {
return this.comment;
}
public void setComment(String comment) {
this.comment = comment;
}
        public Set getJzGtypes() {
return this.jzGtypes;
}
public void setJzGtypes(Set jzGtypes) {
this.jzGtypes = jzGtypes;
}
public abstract class JzGtype implements java.io.Serializable {
private Integer gtId;
private JzBusiness jzBusiness;
private String gtName;
public Integer getGtId() {
return this.gtId;
}
public void setGtId(Integer gtId) {
this.gtId = gtId;
}
public JzBusiness getJzBusiness() {
return this.jzBusiness;
}
public void setJzBusiness(JzBusiness jzBusiness) {
this.jzBusiness = jzBusiness;
}
public String getGtName() {
return this.gtName;
}
public void setGtName(String gtName) {
this.gtName = gtName;
}现在需要根据JzBusiness的Id来分页查询JzGtype即:
public List getAllGuestype(int currentPage, int lineSize,int bsid) {
List all = null;
JzBusiness business = getJzBusiness(bsid);
String hql = "FROM JzGtype AS q where jzBusiness = " ;
Query q = super.getSession().createQuery(hql);
q.setFirstResult((currentPage - 1) * lineSize);
q.setMaxResults(lineSize);
all = q.list() ;
return all;
}
怎么写呢?

解决方案 »

  1.   

    String hql = "FROM JzBusiness AS j where j.bsId = "+bsid ;
      

  2.   

    上面的不好
    String hql = "FROM JzGtype AS j where j.jzBusiness.bsId = "+ bsid;
      

  3.   

    public List getAllGuestype(int currentPage, int lineSize,int bsid) { 
     List all = null; 
     String sql = "FROM JzGtype AS q where q.jzBusiness.id ="+bsid
     Query q = super.getSession().createQuery(hql); 
     q.setFirstResult((currentPage - 1) * lineSize); 
     q.setMaxResults(lineSize); 
     all = q.list() ; 
     return all; 
    }
      

  4.   

    不是太明白lz想要问什么,sql不是很简单吗?
    JzBusiness business = getJzBusiness(bsid); 
    String hql = "FROM JzGtype AS q where q.jzBusiness = ?" ;
    Query q = super.getSession().createQuery(hql); 
    q.setParameter(0,business);
    ...