T1:(多的一方)
public class T1 implements java.io.Serializable {
// Fields
private Long id;
private T t;
private String name;
private Date year;
// Constructors
/** default constructor */
public T1() {
}
/** full constructor */
public T1(T t, String name, Date year) {
this.t = t;
this.name = name;
this.year = year;
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public T getT() {
return this.t;
}
public void setT(T t) {
this.t = t;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Date getYear() {
return this.year;
}
public void setYear(Date year) {
this.year = year;
}
}
one的一方:
package com.model;
import java.util.HashSet;
import java.util.Set;
/**
* T generated by MyEclipse Persistence Tools
*/
public class T implements java.io.Serializable {
// Fields
private Long id;
private String name;
private Set t1s = new HashSet(0);
// Constructors
/** default constructor */
public T() {
}
/** full constructor */
public T(String name, Set t1s) {
this.name = name;
this.t1s = t1s;
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set getT1s() {
return this.t1s;
}
public void setT1s(Set t1s) {
this.t1s = t1s;
}
}
查询语句:
Session session = HibernateSessionFactory.getSession();
String hql = "from T t where t.id =1 order by t.t1s.year asc";
Query q = session.createQuery(hql);
List<T> list = q.list();
for(T t : list){
System.out.println("t\t" + t.getId());
Set<T1> set = t.getT1s();
for(T1 t1 : set){
System.out.println("t1\t" + t1.getId());
}
}
输出结果:
Hibernate: select t0_.ID as ID0_, t0_.NAME as NAME0_ from SCOTT.T t0_, SCOTT.T1 t1s1_ where t0_.ID=t1s1_.T_ID and t0_.ID=1 order by t1s1_.YEAR asc
t 1
Hibernate: select t1s0_.T_ID as T2_1_, t1s0_.ID as ID1_, t1s0_.ID as ID1_0_, t1s0_.T_ID as T2_1_0_, t1s0_.NAME as NAME1_0_, t1s0_.YEAR as YEAR1_0_ from SCOTT.T1 t1s0_ where t1s0_.T_ID=?
t1 4
t1 5
t1 3
t 1
t1 4
t1 5
t1 3
t 1
t1 4
t1 5
t1 3
查看输出结果,发现,每一条数据都输出了三次。
仔细看了下hql语句:
Hibernate: select t0_.ID as ID0_, t0_.NAME as NAME0_ from SCOTT.T t0_, SCOTT.T1 t1s1_ where t0_.ID=t1s1_.T_ID and t0_.ID=1 order by t1s1_.YEAR asc
发现,竟然是交叉查询。
看来,想在一的一端(T)对多(T1)的一端里面的数据进行排序是不行的。阿门,只能用其他的方式了!