Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Criteria crit = session.createCriteria(Department.class, "dep")
- .createAlias("dep.employeesSet", "empl")
- .setProjection(Projections.projectionList()
- .add(Projections.property("dep.id"), "id")
- .add(Projections.property("dep.name"), "name")
- .add(Projections.count("empl.id"), "quantity")
- .add(Property.forName("dep.name").group(), "name"))
- .addOrder(Order.asc("quantity"))
- .setResultTransformer(Transformers.aliasToBean(IndexPageView.class));
- indexPageViewList = (ArrayList<IndexPageView>) crit.list();
- @Entity
- @Table(name = "EMPLOYEE")
- public class Employee implements Serializable {
- @Id
- @Column(name = "id")
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Integer id;
- @ManyToOne
- @JoinColumn (name = "deapartment_id", updatable = true)
- private Department department;
- @Column(name = "age")
- private String age;
- @Column(name = "name", length = 25)
- private String name;
- public Employee() {
- }
- public Employee(Department department, String age, String name) {
- this.department = department;
- this.age = age;
- this.name = name;
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Department getDepartment() {
- return department;
- }
- public void setDepartment(Department department) {
- this.department = department;
- }
- @Override
- public String toString() {
- return "Employee{" +
- " id=" + id +
- ", name=" + name +
- ", age=" + age +
- " }n";
- }
- }
- @Entity
- @Table(name = "DEPARTMENT")
- public class Department implements Serializable {
- @Id
- @Column(name = "id", unique = true)
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Integer id;
- @Column(name = "name", unique = false, updatable = true, length = 45)
- private String name;
- @OneToMany(mappedBy = "department", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
- @OnDelete(action = OnDeleteAction.CASCADE)
- private Set<Employee> employeesSet;
- public Department() {
- }
- public Department(String name) {
- this.name = name;
- }
- public Department(int id, String name) {
- this.id = id;
- this.name = name;
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Set<Employee> getEmployeesSet() {
- return employeesSet;
- }
- public void setEmployeesSet(Set<Employee> employeesSet) {
- this.employeesSet = employeesSet;
- }
- @Override
- public String toString() {
- return "Department{" +
- "id=" + id +
- ", name=" + name + "}n";
- }
- }
- public class IndexPageView implements Serializable{
- private int id;
- private int quantity;
- private String name;
- public IndexPageView(){
- }
- public IndexPageView(int id, String name, int quantity){
- this.id = id;
- this.name = name;
- this.quantity = quantity;
- }
- public String getName() {
- return name;
- }
- public int getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public Integer getQuantity() {
- return quantity;
- }
- public void setQuantity(Integer quantity) {
- this.quantity = quantity;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return "IndexPageView{" +
- "id=" + id +
- ", name=" + name +
- ", quantity=" + quantity + "}n";
- }
- }
- Criteria crit = session.createCriteria(Department.class, "dep")
- .createAlias("employeesSet", "empl", JoinType.LEFT_OUTER_JOIN )
- .setProjection(Projections.projectionList()
- .add(Projections.property("dep.id"), "id")
- .add(Projections.property("dep.name"), "name")
- .add(Projections.count("empl.id"), "quantity")
- .add(Property.forName("dep.name").group(), "name"))
- .addOrder(Order.asc("quantity"))
- .setResultTransformer(Transformers.aliasToBean(IndexPageView.class));
- indexPageViewList = (ArrayList<IndexPageView>) crit.list();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement