Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. Criteria crit = session.createCriteria(Department.class, "dep")
  2. .createAlias("dep.employeesSet", "empl")
  3. .setProjection(Projections.projectionList()
  4. .add(Projections.property("dep.id"), "id")
  5. .add(Projections.property("dep.name"), "name")
  6. .add(Projections.count("empl.id"), "quantity")
  7. .add(Property.forName("dep.name").group(), "name"))
  8. .addOrder(Order.asc("quantity"))
  9. .setResultTransformer(Transformers.aliasToBean(IndexPageView.class));
  10. indexPageViewList = (ArrayList<IndexPageView>) crit.list();
  11.  
  12. @Entity
  13. @Table(name = "EMPLOYEE")
  14. public class Employee implements Serializable {
  15. @Id
  16. @Column(name = "id")
  17. @GeneratedValue(strategy = GenerationType.IDENTITY)
  18. private Integer id;
  19.  
  20. @ManyToOne
  21. @JoinColumn (name = "deapartment_id", updatable = true)
  22. private Department department;
  23.  
  24. @Column(name = "age")
  25. private String age;
  26.  
  27. @Column(name = "name", length = 25)
  28. private String name;
  29.  
  30. public Employee() {
  31. }
  32.  
  33. public Employee(Department department, String age, String name) {
  34. this.department = department;
  35. this.age = age;
  36. this.name = name;
  37. }
  38.  
  39.  
  40. public Integer getId() {
  41. return id;
  42. }
  43.  
  44. public void setId(Integer id) {
  45. this.id = id;
  46. }
  47.  
  48. public String getAge() {
  49. return age;
  50. }
  51.  
  52. public void setAge(String age) {
  53. this.age = age;
  54. }
  55.  
  56. public String getName() {
  57. return name;
  58. }
  59.  
  60. public void setName(String name) {
  61. this.name = name;
  62. }
  63.  
  64. public Department getDepartment() {
  65. return department;
  66. }
  67.  
  68. public void setDepartment(Department department) {
  69. this.department = department;
  70. }
  71.  
  72.  
  73. @Override
  74. public String toString() {
  75. return "Employee{" +
  76. " id=" + id +
  77. ", name=" + name +
  78. ", age=" + age +
  79. " }n";
  80. }
  81. }
  82.  
  83. @Entity
  84. @Table(name = "DEPARTMENT")
  85. public class Department implements Serializable {
  86.  
  87. @Id
  88. @Column(name = "id", unique = true)
  89. @GeneratedValue(strategy = GenerationType.IDENTITY)
  90. private Integer id;
  91.  
  92. @Column(name = "name", unique = false, updatable = true, length = 45)
  93. private String name;
  94.  
  95. @OneToMany(mappedBy = "department", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  96. @OnDelete(action = OnDeleteAction.CASCADE)
  97. private Set<Employee> employeesSet;
  98.  
  99. public Department() {
  100. }
  101.  
  102. public Department(String name) {
  103. this.name = name;
  104. }
  105.  
  106. public Department(int id, String name) {
  107. this.id = id;
  108. this.name = name;
  109. }
  110.  
  111. public Integer getId() {
  112. return id;
  113. }
  114.  
  115. public void setId(Integer id) {
  116. this.id = id;
  117. }
  118.  
  119. public String getName() {
  120. return name;
  121. }
  122.  
  123. public void setName(String name) {
  124. this.name = name;
  125. }
  126.  
  127. public Set<Employee> getEmployeesSet() {
  128. return employeesSet;
  129. }
  130.  
  131. public void setEmployeesSet(Set<Employee> employeesSet) {
  132. this.employeesSet = employeesSet;
  133. }
  134.  
  135. @Override
  136. public String toString() {
  137. return "Department{" +
  138. "id=" + id +
  139. ", name=" + name + "}n";
  140. }
  141. }
  142.  
  143. public class IndexPageView implements Serializable{
  144.  
  145. private int id;
  146. private int quantity;
  147. private String name;
  148.  
  149. public IndexPageView(){
  150. }
  151.  
  152. public IndexPageView(int id, String name, int quantity){
  153. this.id = id;
  154. this.name = name;
  155. this.quantity = quantity;
  156. }
  157.  
  158. public String getName() {
  159. return name;
  160. }
  161.  
  162. public int getId() {
  163. return id;
  164. }
  165.  
  166. public void setId(Integer id) {
  167. this.id = id;
  168. }
  169.  
  170. public Integer getQuantity() {
  171. return quantity;
  172. }
  173.  
  174. public void setQuantity(Integer quantity) {
  175. this.quantity = quantity;
  176. }
  177.  
  178. public void setName(String name) {
  179. this.name = name;
  180. }
  181.  
  182. @Override
  183. public String toString() {
  184. return "IndexPageView{" +
  185. "id=" + id +
  186. ", name=" + name +
  187. ", quantity=" + quantity + "}n";
  188. }
  189. }
  190.  
  191. Criteria crit = session.createCriteria(Department.class, "dep")
  192. .createAlias("employeesSet", "empl", JoinType.LEFT_OUTER_JOIN )
  193. .setProjection(Projections.projectionList()
  194. .add(Projections.property("dep.id"), "id")
  195. .add(Projections.property("dep.name"), "name")
  196. .add(Projections.count("empl.id"), "quantity")
  197. .add(Property.forName("dep.name").group(), "name"))
  198. .addOrder(Order.asc("quantity"))
  199. .setResultTransformer(Transformers.aliasToBean(IndexPageView.class));
  200. indexPageViewList = (ArrayList<IndexPageView>) crit.list();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement