Hibernate Criteria查询之多表连接分页

最近尝试用Hibernate的Criteria查询实现多表连接下的分页,发现一些Hibernate的奇怪问题:多表连接后的分页求总条数始终报错,检查生成的sql也不正确。研究许久找到问题所在,特贴如下:

Mysql脚本如下:

学生 测试数据,请勿见笑:)

[sql]  view plain  copy
  1. /*  
  2. Navicat MySQL Data Transfer  
  3.   
  4. Source Server         : localhost  
  5. Source Server Version : 50018  
  6. Source Host           : localhost:3306  
  7. Source Database       : house  
  8.   
  9. Target Server Type    : MYSQL  
  10. Target Server Version : 50018  
  11. File Encoding         : 65001  
  12.   
  13. Date: 2011-08-10 13:38:12  
  14. */  
  15.   
  16. SET FOREIGN_KEY_CHECKS=0;  
  17. -- ----------------------------  
  18. -- Table structure for `district`  
  19. -- ----------------------------  
  20. DROP TABLE IF EXISTS `district`;  
  21. CREATE TABLE `district` (  
  22.   `ID` int(11) NOT NULL auto_increment,  
  23.   `namevarchar(50) NOT NULL,  
  24.   PRIMARY KEY  (`ID`)  
  25. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
  26.   
  27. -- ----------------------------  
  28. -- Records of district  
  29. -- ----------------------------  
  30. INSERT INTO `district` VALUES ('1''青羊区');  
  31. INSERT INTO `district` VALUES ('2''金牛区');  
  32. INSERT INTO `district` VALUES ('3''抚琴小区');  
  33. INSERT INTO `district` VALUES ('4''朝阳区');  
  34. INSERT INTO `district` VALUES ('5''阳光小区');  
  35. INSERT INTO `district` VALUES ('6''名扬小区');  
  36.   
  37. -- ----------------------------  
  38. -- Table structure for `house`  
  39. -- ----------------------------  
  40. DROP TABLE IF EXISTS `house`;  
  41. CREATE TABLE `house` (  
  42.   `ID` int(11) NOT NULL auto_increment,  
  43.   `user_id` int(11) NOT NULL,  
  44.   `type_id` int(11) NOT NULL,  
  45.   `title` varchar(50) NOT NULL,  
  46.   `description` varchar(2000) NOT NULL,  
  47.   `price` int(11) NOT NULL,  
  48.   `pubdate` date NOT NULL,  
  49.   `floorage` int(11) NOT NULL,  
  50.   `contact` varchar(255) NOT NULL,  
  51.   `street_id` int(11) NOT NULL,  
  52.   PRIMARY KEY  (`ID`),  
  53.   KEY `fk_user_house` (`user_id`),  
  54.   KEY `fk_type_house` (`type_id`),  
  55.   KEY `fk_street_house` (`street_id`),  
  56.   CONSTRAINT `fk_street_house` FOREIGN KEY (`street_id`) REFERENCES `street` (`ID`),  
  57.   CONSTRAINT `fk_type_house` FOREIGN KEY (`type_id`) REFERENCES `type` (`ID`),  
  58.   CONSTRAINT `fk_user_house` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)  
  59. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
  60.   
  61. -- ----------------------------  
  62. -- Records of house  
  63. -- ----------------------------  
  64. INSERT INTO `house` VALUES ('1''1''1''第一个房子''很好,向阳,方便舒适''1234''2011-07-26''123''1354135423''1');  
  65. INSERT INTO `house` VALUES ('2''2''2''第二个房子''简单家具,宽敞明亮''4543''2011-07-04''343''1435465656''3');  
  66. INSERT INTO `house` VALUES ('3''1''1''第三个房子''干净宽敞明亮''123''2011-07-13''123''1342533333''3');  
  67. INSERT INTO `house` VALUES ('4''3''4''第四个房子''和低速复苏阿发塑封股''214''2011-07-04''3444''323243423442''5');  
  68. INSERT INTO `house` VALUES ('5''3''3''第五间房''是不是哦马哈好似奥挥洒出''12''2011-07-03''878''13541234567''3');  
  69. INSERT INTO `house` VALUES ('6''4''2''第六间房''非hiusa公司udgfbsdugf 岁噶''344''2011-07-12''546''1423343243''3');  
  70. INSERT INTO `house` VALUES ('8''1''4''wrwegrhtrhgh''dgv hyhhhhhhhhhhhhh''44''2011-07-11''676''154354545455''1');  
  71. INSERT INTO `house` VALUES ('9''1''2''gfdgggggggggggggg''afdgdsgdgdf''2342''2011-06-27''12423''1535436546''1');  
  72. INSERT INTO `house` VALUES ('10''1''2''dfegggg''fdfdgfd''21423''2011-07-10''1243''243254354''1');  
  73. INSERT INTO `house` VALUES ('11''2''2''rrrrr''vdfgrdtg''2423''2011-07-11''232''13423432435''2');  
  74.   
  75. -- ----------------------------  
  76. -- Table structure for `street`  
  77. -- ----------------------------  
  78. DROP TABLE IF EXISTS `street`;  
  79. CREATE TABLE `street` (  
  80.   `ID` int(11) NOT NULL auto_increment,  
  81.   `namevarchar(50) NOT NULL,  
  82.   `district_id` int(11) NOT NULL,  
  83.   PRIMARY KEY  (`ID`),  
  84.   KEY `fk_street_district_id` (`district_id`),  
  85.   CONSTRAINT `fk_street_district_id` FOREIGN KEY (`district_id`) REFERENCES `district` (`ID`)  
  86. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
  87.   
  88. -- ----------------------------  
  89. -- Records of street  
  90. -- ----------------------------  
  91. INSERT INTO `street` VALUES ('1''东南路''1');  
  92. INSERT INTO `street` VALUES ('2''知春路''1');  
  93. INSERT INTO `street` VALUES ('3''中关村大街''2');  
  94. INSERT INTO `street` VALUES ('4''学院路''3');  
  95. INSERT INTO `street` VALUES ('5''朝阳路''3');  
  96. INSERT INTO `street` VALUES ('6''成荫路''4');  
  97. INSERT INTO `street` VALUES ('7''哈哈路''3');  
  98.   
  99. -- ----------------------------  
  100. -- Table structure for `type`  
  101. -- ----------------------------  
  102. DROP TABLE IF EXISTS `type`;  
  103. CREATE TABLE `type` (  
  104.   `ID` int(11) NOT NULL auto_increment,  
  105.   `namevarchar(10) NOT NULL,  
  106.   PRIMARY KEY  (`ID`)  
  107. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
  108.   
  109. -- ----------------------------  
  110. -- Records of type  
  111. -- ----------------------------  
  112. INSERT INTO `type` VALUES ('1''一室一厅');  
  113. INSERT INTO `type` VALUES ('2''两室一厅');  
  114. INSERT INTO `type` VALUES ('3''三室一厅');  
  115. INSERT INTO `type` VALUES ('4''四室一厅');  
  116. INSERT INTO `type` VALUES ('5''两室两厅');  
  117.   
  118. -- ----------------------------  
  119. -- Table structure for `users`  
  120. -- ----------------------------  
  121. DROP TABLE IF EXISTS `users`;  
  122. CREATE TABLE `users` (  
  123.   `id` int(11) NOT NULL auto_increment,  
  124.   `namevarchar(50) NOT NULL,  
  125.   `passwordvarchar(50) NOT NULL,  
  126.   `telephone` varchar(15) NOT NULL,  
  127.   `userName` varchar(50) NOT NULL,  
  128.   `idAdmin` varchar(5) NOT NULL,  
  129.   PRIMARY KEY  (`id`)  
  130. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
  131.   
  132. -- ----------------------------  
  133. -- Records of users  
  134. -- ----------------------------  
  135. INSERT INTO `users` VALUES ('1''admin''12345''13541305274''Admin''1');  
  136. INSERT INTO `users` VALUES ('2''zhang''12345''12345678444''张小花''2');  
  137. INSERT INTO `users` VALUES ('3''li''12345''12234242354''zhngxaida''1');  
  138. INSERT INTO `users` VALUES ('4''luo''luo''143435y4385''hsafgdsavds''1');  
  139. INSERT INTO `users` VALUES ('5''huang''huang''14235435465''hdusigfsu''1');  


现有实体类如下:

区实体类:

[java]  view plain  copy
  1. package org.accp.mhouse.entities;  
  2.   
  3. import java.util.HashSet;  
  4. import java.util.Set;  
  5. import javax.persistence.CascadeType;  
  6. import javax.persistence.Column;  
  7. import javax.persistence.Entity;  
  8. import javax.persistence.FetchType;  
  9. import javax.persistence.GeneratedValue;  
  10. import static javax.persistence.GenerationType.IDENTITY;  
  11. import javax.persistence.Id;  
  12. import javax.persistence.OneToMany;  
  13. import javax.persistence.Table;  
  14.   
  15. /** 
  16.  * District entity. @author MyEclipse Persistence Tools 
  17.  */  
  18. @Entity  
  19. @Table(name = "district", catalog = "house")  
  20. public class District implements java.io.Serializable {  
  21.   
  22.     // Fields  
  23.   
  24.     private Integer id;  
  25.     private String name;  
  26.     private Set<Street> streets = new HashSet<Street>(0);  
  27.   
  28.     // Constructors  
  29.   
  30.     /** default constructor */  
  31.     public District() {  
  32.     }  
  33.   
  34.     /** minimal constructor */  
  35.     public District(String name) {  
  36.         this.name = name;  
  37.     }  
  38.   
  39.     /** full constructor */  
  40.     public District(String name, Set<Street> streets) {  
  41.         this.name = name;  
  42.         this.streets = streets;  
  43.     }  
  44.   
  45.     // Property accessors  
  46.     @Id  
  47.     @GeneratedValue(strategy = IDENTITY)  
  48.     @Column(name = "ID", unique = true, nullable = false)  
  49.     public Integer getId() {  
  50.         return this.id;  
  51.     }  
  52.   
  53.     public void setId(Integer id) {  
  54.         this.id = id;  
  55.     }  
  56.   
  57.     @Column(name = "name", nullable = false, length = 50)  
  58.     public String getName() {  
  59.         return this.name;  
  60.     }  
  61.   
  62.     public void setName(String name) {  
  63.         this.name = name;  
  64.     }  
  65.   
  66.     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "district")  
  67.     public Set<Street> getStreets() {  
  68.         return this.streets;  
  69.     }  
  70.   
  71.     public void setStreets(Set<Street> streets) {  
  72.         this.streets = streets;  
  73.     }  
  74.   
  75. }  

街道实体类:

[java]  view plain  copy
  1. package org.accp.mhouse.entities;  
  2.   
  3. import java.util.HashSet;  
  4. import java.util.Set;  
  5. import javax.persistence.CascadeType;  
  6. import javax.persistence.Column;  
  7. import javax.persistence.Entity;  
  8. import javax.persistence.FetchType;  
  9. import javax.persistence.GeneratedValue;  
  10. import static javax.persistence.GenerationType.IDENTITY;  
  11. import javax.persistence.Id;  
  12. import javax.persistence.JoinColumn;  
  13. import javax.persistence.ManyToOne;  
  14. import javax.persistence.OneToMany;  
  15. import javax.persistence.Table;  
  16.   
  17. /** 
  18.  * Street entity. @author MyEclipse Persistence Tools 
  19.  */  
  20. @Entity  
  21. @Table(name = "street", catalog = "house")  
  22. public class Street implements java.io.Serializable {  
  23.   
  24.     // Fields  
  25.   
  26.     private Integer id;  
  27.     private District district;  
  28.     private String name;  
  29.     private Set<House> houses = new HashSet<House>(0);  
  30.   
  31.     // Constructors  
  32.   
  33.     /** default constructor */  
  34.     public Street() {  
  35.     }  
  36.   
  37.     /** minimal constructor */  
  38.     public Street(District district, String name) {  
  39.         this.district = district;  
  40.         this.name = name;  
  41.     }  
  42.   
  43.     /** full constructor */  
  44.     public Street(District district, String name, Set<House> houses) {  
  45.         this.district = district;  
  46.         this.name = name;  
  47.         this.houses = houses;  
  48.     }  
  49.   
  50.     // Property accessors  
  51.     @Id  
  52.     @GeneratedValue(strategy = IDENTITY)  
  53.     @Column(name = "ID", unique = true, nullable = false)  
  54.     public Integer getId() {  
  55.         return this.id;  
  56.     }  
  57.   
  58.     public void setId(Integer id) {  
  59.         this.id = id;  
  60.     }  
  61.   
  62.     @ManyToOne(fetch = FetchType.LAZY)  
  63.     @JoinColumn(name = "district_id", nullable = false)  
  64.     public District getDistrict() {  
  65.         return this.district;  
  66.     }  
  67.   
  68.     public void setDistrict(District district) {  
  69.         this.district = district;  
  70.     }  
  71.   
  72.     @Column(name = "name", nullable = false, length = 50)  
  73.     public String getName() {  
  74.         return this.name;  
  75.     }  
  76.   
  77.     public void setName(String name) {  
  78.         this.name = name;  
  79.     }  
  80.   
  81.     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "street")  
  82.     public Set<House> getHouses() {  
  83.         return this.houses;  
  84.     }  
  85.   
  86.     public void setHouses(Set<House> houses) {  
  87.         this.houses = houses;  
  88.     }  
  89.   
  90. }  


房屋类型实体类:

[java]  view plain  copy
  1. package org.accp.mhouse.entities;  
  2.   
  3. import java.util.HashSet;  
  4. import java.util.Set;  
  5. import javax.persistence.CascadeType;  
  6. import javax.persistence.Column;  
  7. import javax.persistence.Entity;  
  8. import javax.persistence.FetchType;  
  9. import javax.persistence.GeneratedValue;  
  10. import static javax.persistence.GenerationType.IDENTITY;  
  11. import javax.persistence.Id;  
  12. import javax.persistence.OneToMany;  
  13. import javax.persistence.Table;  
  14.   
  15. /** 
  16.  * Type entity. @author MyEclipse Persistence Tools 
  17.  */  
  18. @Entity  
  19. @Table(name = "type", catalog = "house")  
  20. public class Type implements java.io.Serializable {  
  21.   
  22.     // Fields  
  23.   
  24.     private Integer id;  
  25.     private String name;  
  26.     private Set<House> houses = new HashSet<House>(0);  
  27.   
  28.     // Constructors  
  29.   
  30.     /** default constructor */  
  31.     public Type() {  
  32.     }  
  33.   
  34.     /** minimal constructor */  
  35.     public Type(String name) {  
  36.         this.name = name;  
  37.     }  
  38.   
  39.     /** full constructor */  
  40.     public Type(String name, Set<House> houses) {  
  41.         this.name = name;  
  42.         this.houses = houses;  
  43.     }  
  44.   
  45.     // Property accessors  
  46.     @Id  
  47.     @GeneratedValue(strategy = IDENTITY)  
  48.     @Column(name = "ID", unique = true, nullable = false)  
  49.     public Integer getId() {  
  50.         return this.id;  
  51.     }  
  52.   
  53.     public void setId(Integer id) {  
  54.         this.id = id;  
  55.     }  
  56.   
  57.     @Column(name = "name", nullable = false, length = 10)  
  58.     public String getName() {  
  59.         return this.name;  
  60.     }  
  61.   
  62.     public void setName(String name) {  
  63.         this.name = name;  
  64.     }  
  65.   
  66.     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "type")  
  67.     public Set<House> getHouses() {  
  68.         return this.houses;  
  69.     }  
  70.   
  71.     public void setHouses(Set<House> houses) {  
  72.         this.houses = houses;  
  73.     }  
  74.   
  75. }  


用户实体类:

[java]  view plain  copy
  1. package org.accp.mhouse.entities;  
  2.   
  3. import java.util.HashSet;  
  4. import java.util.Set;  
  5. import javax.persistence.CascadeType;  
  6. import javax.persistence.Column;  
  7. import javax.persistence.Entity;  
  8. import javax.persistence.FetchType;  
  9. import javax.persistence.GeneratedValue;  
  10. import static javax.persistence.GenerationType.IDENTITY;  
  11. import javax.persistence.Id;  
  12. import javax.persistence.OneToMany;  
  13. import javax.persistence.Table;  
  14.   
  15. /** 
  16.  * Users entity. @author MyEclipse Persistence Tools 
  17.  */  
  18. @Entity  
  19. @Table(name = "users", catalog = "house")  
  20. public class Users implements java.io.Serializable {  
  21.   
  22.     // Fields  
  23.   
  24.     private Integer id;  
  25.     private String name;  
  26.     private String password;  
  27.     private String telephone;  
  28.     private String userName;  
  29.     private String idAdmin;  
  30.     private Set<House> houses = new HashSet<House>(0);  
  31.   
  32.     // Constructors  
  33.   
  34.     /** default constructor */  
  35.     public Users() {  
  36.     }  
  37.   
  38.     /** minimal constructor */  
  39.     public Users(String name, String password, String telephone,  
  40.             String userName, String idAdmin) {  
  41.         this.name = name;  
  42.         this.password = password;  
  43.         this.telephone = telephone;  
  44.         this.userName = userName;  
  45.         this.idAdmin = idAdmin;  
  46.     }  
  47.   
  48.     /** full constructor */  
  49.     public Users(String name, String password, String telephone,  
  50.             String userName, String idAdmin, Set<House> houses) {  
  51.         this.name = name;  
  52.         this.password = password;  
  53.         this.telephone = telephone;  
  54.         this.userName = userName;  
  55.         this.idAdmin = idAdmin;  
  56.         this.houses = houses;  
  57.     }  
  58.   
  59.     // Property accessors  
  60.     @Id  
  61.     @GeneratedValue(strategy = IDENTITY)  
  62.     @Column(name = "id", unique = true, nullable = false)  
  63.     public Integer getId() {  
  64.         return this.id;  
  65.     }  
  66.   
  67.     public void setId(Integer id) {  
  68.         this.id = id;  
  69.     }  
  70.   
  71.     @Column(name = "name", nullable = false, length = 50)  
  72.     public String getName() {  
  73.         return this.name;  
  74.     }  
  75.   
  76.     public void setName(String name) {  
  77.         this.name = name;  
  78.     }  
  79.   
  80.     @Column(name = "password", nullable = false, length = 50)  
  81.     public String getPassword() {  
  82.         return this.password;  
  83.     }  
  84.   
  85.     public void setPassword(String password) {  
  86.         this.password = password;  
  87.     }  
  88.   
  89.     @Column(name = "telephone", nullable = false, length = 15)  
  90.     public String getTelephone() {  
  91.         return this.telephone;  
  92.     }  
  93.   
  94.     public void setTelephone(String telephone) {  
  95.         this.telephone = telephone;  
  96.     }  
  97.   
  98.     @Column(name = "userName", nullable = false, length = 50)  
  99.     public String getUserName() {  
  100.         return this.userName;  
  101.     }  
  102.   
  103.     public void setUserName(String userName) {  
  104.         this.userName = userName;  
  105.     }  
  106.   
  107.     @Column(name = "idAdmin", nullable = false, length = 5)  
  108.     public String getIdAdmin() {  
  109.         return this.idAdmin;  
  110.     }  
  111.   
  112.     public void setIdAdmin(String idAdmin) {  
  113.         this.idAdmin = idAdmin;  
  114.     }  
  115.   
  116.     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "users")  
  117.     public Set<House> getHouses() {  
  118.         return this.houses;  
  119.     }  
  120.   
  121.     public void setHouses(Set<House> houses) {  
  122.         this.houses = houses;  
  123.     }  
  124.   
  125. }  


房屋实体类:

[java]  view plain  copy
  1. package org.accp.mhouse.entities;  
  2.   
  3. import java.util.Date;  
  4. import javax.persistence.Column;  
  5. import javax.persistence.Entity;  
  6. import javax.persistence.FetchType;  
  7. import javax.persistence.GeneratedValue;  
  8. import static javax.persistence.GenerationType.IDENTITY;  
  9. import javax.persistence.Id;  
  10. import javax.persistence.JoinColumn;  
  11. import javax.persistence.ManyToOne;  
  12. import javax.persistence.Table;  
  13. import javax.persistence.Temporal;  
  14. import javax.persistence.TemporalType;  
  15.   
  16. /** 
  17.  * House entity. @author MyEclipse Persistence Tools 
  18.  */  
  19. @Entity  
  20. @Table(name = "house", catalog = "house")  
  21. public class House implements java.io.Serializable {  
  22.   
  23.     // Fields  
  24.   
  25.     private Integer id;  
  26.     private Users users;//many to one  
  27.     private Type type;//many to one  
  28.     private Street street;//many to one  
  29.     private String title;  
  30.     private String description;  
  31.     private Integer price;  
  32.     private Date pubdate;  
  33.     private Integer floorage;  
  34.     private String contact;  
  35.   
  36.     // Constructors  
  37.   
  38.     /** default constructor */  
  39.     public House() {  
  40.     }  
  41.   
  42.     /** full constructor */  
  43.     public House(Users users, Type type, Street street, String title,  
  44.             String description, Integer price, Date pubdate, Integer floorage,  
  45.             String contact) {  
  46.         this.users = users;  
  47.         this.type = type;  
  48.         this.street = street;  
  49.         this.title = title;  
  50.         this.description = description;  
  51.         this.price = price;  
  52.         this.pubdate = pubdate;  
  53.         this.floorage = floorage;  
  54.         this.contact = contact;  
  55.     }  
  56.   
  57.     // Property accessors  
  58.     @Id  
  59.     @GeneratedValue(strategy = IDENTITY)  
  60.     @Column(name = "ID", unique = true, nullable = false)  
  61.     public Integer getId() {  
  62.         return this.id;  
  63.     }  
  64.   
  65.     public void setId(Integer id) {  
  66.         this.id = id;  
  67.     }  
  68.   
  69.     @ManyToOne(fetch = FetchType.LAZY)  
  70.     @JoinColumn(name = "user_id", nullable = false)  
  71.     public Users getUsers() {  
  72.         return this.users;  
  73.     }  
  74.   
  75.     public void setUsers(Users users) {  
  76.         this.users = users;  
  77.     }  
  78.   
  79.     @ManyToOne(fetch = FetchType.LAZY)  
  80.     @JoinColumn(name = "type_id", nullable = false)  
  81.     public Type getType() {  
  82.         return this.type;  
  83.     }  
  84.   
  85.     public void setType(Type type) {  
  86.         this.type = type;  
  87.     }  
  88.   
  89.     @ManyToOne(fetch = FetchType.LAZY)  
  90.     @JoinColumn(name = "street_id", nullable = false)  
  91.     public Street getStreet() {  
  92.         return this.street;  
  93.     }  
  94.   
  95.     public void setStreet(Street street) {  
  96.         this.street = street;  
  97.     }  
  98.   
  99.     @Column(name = "title", nullable = false, length = 50)  
  100.     public String getTitle() {  
  101.         return this.title;  
  102.     }  
  103.   
  104.     public void setTitle(String title) {  
  105.         this.title = title;  
  106.     }  
  107.   
  108.     @Column(name = "description", nullable = false, length = 2000)  
  109.     public String getDescription() {  
  110.         return this.description;  
  111.     }  
  112.   
  113.     public void setDescription(String description) {  
  114.         this.description = description;  
  115.     }  
  116.   
  117.     @Column(name = "price", nullable = false)  
  118.     public Integer getPrice() {  
  119.         return this.price;  
  120.     }  
  121.   
  122.     public void setPrice(Integer price) {  
  123.         this.price = price;  
  124.     }  
  125.   
  126.     @Temporal(TemporalType.DATE)  
  127.     @Column(name = "pubdate", nullable = false, length = 10)  
  128.     public Date getPubdate() {  
  129.         return this.pubdate;  
  130.     }  
  131.   
  132.     public void setPubdate(Date pubdate) {  
  133.         this.pubdate = pubdate;  
  134.     }  
  135.   
  136.     @Column(name = "floorage", nullable = false)  
  137.     public Integer getFloorage() {  
  138.         return this.floorage;  
  139.     }  
  140.   
  141.     public void setFloorage(Integer floorage) {  
  142.         this.floorage = floorage;  
  143.     }  
  144.   
  145.     @Column(name = "contact", nullable = false)  
  146.     public String getContact() {  
  147.         return this.contact;  
  148.     }  
  149.   
  150.     public void setContact(String contact) {  
  151.         this.contact = contact;  
  152.     }  
  153.   
  154. }  


查询示范1,查询区为“xxx”区的所有房屋信息,并抓取查询房屋对象关联的街道对象,街道对象关联的区对象

首先查询总条数,通常思维习惯如下:

[java]  view plain  copy
  1. //求总条数  
  2.             session  
  3.                 .createCriteria(House.class)  
  4.                 .setFetchMode("street", org.hibernate.FetchMode.JOIN)  
  5.                 .setFetchMode("street.district",org.hibernate.FetchMode.JOIN)  
  6.                 .createAlias("street.district""d")  
  7.                 .add(Restrictions.eq("d.name""青羊区"))  
  8.                 .setProjection(Projections.rowCount())  
  9.                 .uniqueResult();  

很遗憾,查询结果大失所望:

控制异常如下:

[java]  view plain  copy
  1. Hibernate:   
  2.     select  
  3.         count(*) as y0_   
  4.     from  
  5.         house.house this_   
  6.     where  
  7.         d1_.name=?  
  8. Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query  
  9.     at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)  
  10.     at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)  
  11.     at org.hibernate.loader.Loader.doList(Loader.java:2220)  
  12.     at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2108)  
  13.     at org.hibernate.loader.Loader.list(Loader.java:2103)  
  14.     at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)  
  15.     at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1570)  
  16.     at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)  
  17.     at org.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:305)  
  18.     at org.accp.mhouse.dao.MainModule.main(MainModule.java:164)  
  19. Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'd1_.name' in 'where clause'  
  20.     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)  
  21.     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)  
  22.     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)  
  23.     at java.lang.reflect.Constructor.newInstance(Constructor.java:513)  
  24.     at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)  
  25.     at com.mysql.jdbc.Util.getInstance(Util.java:381)  
  26.     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)  
  27.     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)  
  28.     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)  
  29.     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)  
  30.     at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)  
  31.     at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)  
  32.     at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)  
  33.     at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)  
  34.     at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1885)  
  35.     at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:187)  
  36.     at org.hibernate.loader.Loader.getResultSet(Loader.java:1791)  
  37.     at org.hibernate.loader.Loader.doQuery(Loader.java:674)  
  38.     at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)  
  39.     at org.hibernate.loader.Loader.doList(Loader.java:2217)  
  40.     ... 7 more  


但是以上语句如果是查询结果那是绝对正确的,如下:

[java]  view plain  copy
  1. //结果  
  2.             session  
  3.                 .createCriteria(House.class)  
  4.                 .setFetchMode("street", org.hibernate.FetchMode.JOIN)  
  5.                 .setFetchMode("street.district",org.hibernate.FetchMode.JOIN)  
  6.                 .createAlias("street.district""d")  
  7.                 .add(Restrictions.eq("d.name""青羊区"))  
  8.                 .list();  


控制台输出如下:

[sql]  view plain  copy
  1. Hibernate:   
  2.     select  
  3.         this_.ID as ID2_2_,  
  4.         this_.contact as contact2_2_,  
  5.         this_.description as descript3_2_2_,  
  6.         this_.floorage as floorage2_2_,  
  7.         this_.price as price2_2_,  
  8.         this_.pubdate as pubdate2_2_,  
  9.         this_.street_id as street8_2_2_,  
  10.         this_.title as title2_2_,  
  11.         this_.type_id as type9_2_2_,  
  12.         this_.user_id as user10_2_2_,  
  13.         street3_.ID as ID1_0_,  
  14.         street3_.district_id as district3_1_0_,  
  15.         street3_.name as name1_0_,  
  16.         d1_.ID as ID4_1_,  
  17.         d1_.name as name4_1_   
  18.     from  
  19.         house.house this_   
  20.     inner join  
  21.         house.street street3_   
  22.             on this_.street_id=street3_.ID   
  23.     inner join  
  24.         house.district d1_   
  25.             on street3_.district_id=d1_.ID   
  26.     where  
  27.         d1_.name=?  


就是上面那个求总条数的问题困扰我许久,网络搜索无果,几经查找摸索,正确如下:

求总条数:

[java]  view plain  copy
  1. //求总条数  
  2.             session  
  3.                 .createCriteria(House.class)  
  4.                 .createAlias("street","st",CriteriaSpecification.INNER_JOIN)  
  5.                 .createAlias("st.district","d",CriteriaSpecification.INNER_JOIN)  
  6.                 .createAlias("users""u", CriteriaSpecification.INNER_JOIN)  
  7.                 .createAlias("type","t",CriteriaSpecification.INNER_JOIN)  
  8.                 .add(Restrictions.eq("d.name""青羊区"))  
  9.                 .setProjection(Projections.rowCount())  
  10.                 .uniqueResult();  

Hibernate API文档对createAlias()方法的解释:

createAlias

Criteria createAlias(String associationPath,
                     String alias,
                     int joinType)
                     throws HibernateException
Join an association using the specified join-type, assigning an alias to the joined association.

The joinType is expected to be one of CriteriaSpecification.INNER_JOIN (the default), CriteriaSpecification.FULL_JOIN, or CriteriaSpecification.LEFT_JOIN.

Parameters:
associationPath - A dot-seperated property path
alias - The alias to assign to the joined association (for later reference).
joinType - The type of join to use.
Returns:
this (for method chaining)
Throws:
HibernateException

很明显上面的查询更加复杂,房屋关联街道,街道关联区,房屋关联类型,房屋关联用户,筛选条件是区是“青羊区”,我们看查询结果:

[sql]  view plain  copy
  1. Hibernate:   
  2.     select  
  3.         count(*) as y0_   
  4.     from  
  5.         house.house this_   
  6.     inner join  
  7.         house.street st1_   
  8.             on this_.street_id=st1_.ID   
  9.     inner join  
  10.         house.district d2_   
  11.             on st1_.district_id=d2_.ID   
  12.     inner join  
  13.         house.type t4_   
  14.             on this_.type_id=t4_.ID   
  15.     inner join  
  16.         house.users u3_   
  17.             on this_.user_id=u3_.id   
  18.     where  
  19.         d2_.name=?  

猜你喜欢

转载自blog.csdn.net/qq_40285302/article/details/80036015