Hello
i am having the following declarations
Code:
@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "EMPLOYEE_ID")
private Integer id;
@Column(name = "EMPLOYEE_REGISTRATION_NUMBER")
private String registrationNumber;
@Column(name = "EMPLOYEE_FIRST_NAME")
private String firstName;
@Column(name = "EMPLOYEE_LAST_NAME")
private String lastName;
@Column(name = "EMPLOYEE_FATHER_NAME")
private String fatherName;
@Column(name = "EMPLOYEE_SEX")
@org.hibernate.annotations.Type(type="yes_no")
private Boolean male = false;
@OneToMany(targetEntity=EmployeeCategory.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="employee")
[b]@OrderBy("date")[/b]
@JoinColumn(name = "EMPLOYEE_ID")
private Set categories = new HashSet();
}
@Table(name = "EMPLOYEE_CATEGORY")
public class EmployeeCategory implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "EMPLOYEE_CATEGORY_ID")
private Integer id;
@Column(name = "CATEGORY_DATE")
@Temporal(javax.persistence.TemporalType.DATE)
private Date date;
@ManyToOne(targetEntity=Employee.class)
@JoinColumn(name = "EMPLOYEE_ID",nullable=true)
private Employee employee = new Employee();
@ManyToOne(targetEntity=CategoryType.class, fetch=FetchType.EAGER)
@JoinColumn(name = "EMPLOYEE_CATEGORY_TYPE_ID")
private CategoryType type = new CategoryType();
}
and when i am getting Employees based on following criteria
Code:
Criteria crit = session.createCriteria(Employee.class, "e");
crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
crit.setFetchMode("permissions", FetchMode.JOIN);
// lastName
if(criteria.getLastName() != null &&
criteria.getLastName().length() > 0) {
crit.add( Restrictions.ilike("lastName", criteria.getLastName()+"%"));
}
// firstName
if(criteria.getFirstName() != null &&
criteria.getFirstName().length() > 0) {
crit.add( Restrictions.ilike("firstName", criteria.getFirstName()+"%"));
}
crit.addOrder(Order.asc("lastName"));
crit.addOrder(Order.asc("firstName"));
crit.addOrder(Order.asc("fatherName"));
i am getting the list of employees first sort by category.date and then sort by lastName,firstName, fatherName.
The following sql is from hibernate
Code:
Hibernate:
select
this_.EMPLOYEE_ID as EMPLOYEE1_0_8_,
this_.EMPLOYEE_FAX as EMPLOYEE2_0_8_,
this_.EMPLOYEE_PHONE as EMPLOYEE3_0_8_,
this_.EMPLOYEE_CITY as EMPLOYEE4_0_8_,
this_.EMPLOYEE_STREET as EMPLOYEE5_0_8_,
this_.EMPLOYEE_ZIPCODE as EMPLOYEE6_0_8_,
this_.EMPLOYEE_FATHER_NAME as EMPLOYEE7_0_8_,
this_.EMPLOYEE_FIRST_NAME as EMPLOYEE8_0_8_,
this_.EMPLOYEE_LAST_NAME as EMPLOYEE9_0_8_,
this_.EMPLOYEE_SEX as EMPLOYEE10_0_8_,
this_.EMPLOYEE_REGISTRATION_NUMBER as EMPLOYEE11_0_8_,
categories2_.EMPLOYEE_ID as EMPLOYEE3_10_,
categories2_.EMPLOYEE_CATEGORY_ID as EMPLOYEE1_10_,
categories2_.EMPLOYEE_CATEGORY_ID as EMPLOYEE1_1_0_,
categories2_.CATEGORY_DATE as CATEGORY2_1_0_,
categories2_.EMPLOYEE_ID as EMPLOYEE3_1_0_,
categories2_.EMPLOYEE_CATEGORY_TYPE_ID as EMPLOYEE4_1_0_,
categoryty3_.CATEGORY_TYPE_ID as CATEGORY1_15_1_,
categoryty3_.CATEGORY_TYPE_NAME as CATEGORY2_15_1_,
from
EMPLOYEE this_
left outer join
EMPLOYEE_CATEGORY categories2_
on this_.EMPLOYEE_ID=categories2_.EMPLOYEE_ID
left outer join
CATEGORY_TYPE categoryty3_
on categories2_.EMPLOYEE_CATEGORY_TYPE_ID=categoryty3_.CATEGORY_TYPE_ID
order by
[b]categories2_.CATEGORY_DATE asc,[/b]
this_.EMPLOYEE_LAST_NAME asc,
this_.EMPLOYEE_FIRST_NAME asc,
this_.EMPLOYEE_FATHER_NAME asc
is any way to tell hibernate to sort first by lastName,firstName, fatherName and then by category.date?