当前位置: 代码迷 >> J2EE >> Hibernate JPA 查询 请帮忙解决一下
  详细解决方案

Hibernate JPA 查询 请帮忙解决一下

热度:699   发布时间:2016-04-22 01:38:41.0
Hibernate JPA 查询 请各位大哥帮忙解决一下
[color=#FF0000]问题描述:我想查询Tmodel的记录,但是我想要查询的结果按照评论的次数升序或降序排序。[/color]

//////////////////////////////////////Tmodel 类的定义///////////////////////////////////////////////
@Entity
@Table(name = "tb_tmodel")
public class Tmodel implements java.io.Serializable {
private static final long serialVersionUID = -281504951906599269L;
private Long id;
private String name;
private List<TmodelComment> tmodelComments = new ArrayList<TmodelComment>(0);  

public Tmodel() {
}

  @Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")  
  public Long getId(){
  return this.id;
  }  

  public void setId(Long id){
  this.id = id;
  }

@Column(name = "name", nullable = false)
public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

@OneToMany(cascade=CascadeType.REMOVE,fetch=FetchType.LAZY,mappedBy="tmodel")
@OrderBy("id desc")
public List<TmodelComment> getTmodelComments() {
return tmodelComments;
}

public void setTmodelComments(List<TmodelComment> tmodelComments) {
this.tmodelComments = tmodelComments;

}

///////////////////////////////TModel的评论类:TmodelComment类/////////////////////////////////
@Entity
@Table(name = "tb_tmodel_comment")
public class TmodelComment implements Serializable {
private static final long serialVersionUID = -7131379575770195913L;
private Long id;
private String comment;
  private Tmodel tmodel;

public TmodelComment() {
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

@Column(name = "comment", length = 1024, nullable = false)
public String getComment() {
return comment;
}

public void setComment(String comment) {
this.comment = comment;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "tmodel_id", nullable = false)
public Tmodel getTmodel() {
return tmodel;
}

public void setTmodel(Tmodel tmodel) {
this.tmodel = tmodel;
}

}


------解决方案--------------------
please try this:
SQL code
select modelfrom Tmodel modeljoin model.tmodelComments commentgroup by modelorder by count(comment) asc
  相关解决方案