hibernate配置关联关系 及自定义结果集查询

1.在Question类中这样配置:

@OneToMany(fetch=FetchType.LAZY)
 @JoinColumn(name="question_id")
 @OrderBy(value="create_time desc")
 private Set<Answer> answers;

 

2.在Answer类中这样配置:

 @ManyToOne(fetch=FetchType.LAZY)
 @JoinColumn(name="question_id", insertable=false, updatable=false)
 private Question question;

 

 二、自定义结果集查询

 public List<ImageDto> pageByCategory(String categoryId, int pageNo){
  String hql =  "select i.id as id, i.product_id as product_id, i.path as path,i.postfix as postfix, p.name as name " +
      "from im.gsj.entity.Image i, im.gsj.entity.Product p " +
      "where i.product_id = p.id "+
       "and p.category_id = :categoryId";
  Query query =super.listByHql(hql, pageNo, 12);
  query.setParameter("categoryId", categoryId);
  query.setResultTransformer(Transformers.aliasToBean(ImageDto.class));
  List<ImageDto> imageList= query.list();
  return imageList;
 }

类ImageDto的内容如下:

public class ImageDto {
 private String id;
 private String product_id;
 private String path;
 private String postfix;
 private String name;
 
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getProduct_id() {
  return product_id;
 }
 public void setProduct_id(String product_id) {
  this.product_id = product_id;
 }
 public String getPath() {
  return path;
 }
 public void setPath(String path) {
  this.path = path;
 }
 public String getPostfix() {
  return postfix;
 }
 public void setPostfix(String postfix) {
  this.postfix = postfix;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
}

 

你可能感兴趣的:(Hibernate)