I understand if I use
class Fund{
private Set<FundClass> fundClasses = new LinkedHashSet<FundClass>();
@org.hibernate.annotations.OrderBy(clause="order asc")
public Set<FundClass> getFundClasses() {
return fundClasses;
}
}
then my FundClasses will be in order, because hibernate use linkedHashset behind the scence. while, if I开发者_运维百科 want to use the order by clause in HQL such as
from fund f inner join fetch f.fundClass fc order by fc.name
Though the sql returns the records in the order i want, the set is not when I iterator over it, even I define it as linkedHashset. I have a few queries, the fundclass need to be in diffenet order. sometime by name, sometime by order and some other property, I hope I can do the sort in database instead of in memory. how do I do this?
@Entity
@Table(name="GP")
public class GP {
private Integer id;
private String name;
private List<P> ps;
@Id
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;
}
@OneToMany(mappedBy="gp")
public List<P> getPs() {
return ps;
}
public void setPs(List<P> ps) {
this.ps = ps;
}
}
@Entity
@Table(name="P")
public class P {
private Integer id;
private String name;
private GP gp;
private List<C> cs;
@Id
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;
}
@ManyToOne
public GP getGp() {
return gp;
}
public void setGp(GP gp) {
this.gp = gp;
}
@OneToMany(mappedBy="p")
public List<C> getCs() {
return cs;
}
public void setCs(List<C> cs) {
this.cs = cs;
}
}
@Entity
@Table(name="C")
public class C {
private Integer id;
private String name;
private P p;
@Id
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;
}
@ManyToOne
public P getP() {
return p;
}
public void setP(P p) {
this.p = p;
}
}
the query I run
sessionFactory.getCurrentSession().createQuery("select gp from GP gp inner join fetch gp.ps p inner join fetch p.cs c where c.age< 5 and gp.name like 'john%'").list();
Define it to be a List<FundClass>
. In fact, I think that Query.getResultList()
returns .. a list.
精彩评论