`
jbeduhai
  • 浏览: 498864 次
  • 性别: Icon_minigender_1
  • 来自: 山东
社区版块
存档分类
最新评论

JPA多表查询-构造器运用例子

    博客分类:
  • jpa
阅读更多

多表查询所用到的用来构造list的  java类 SimpleInformation.java

 

package com.business;

import java.util.Date;

public class SimpleInformation {

 private Integer infoid;
 private Integer punishid;
 private String subject; // 标题
 private String reason; // 原因
 private String result; // 结果
 private Integer complaintresult;
 private Integer readcount;// 阅读次数
 private Integer commentcount;// 评论次数
 private Integer scorenumber;// 评分分数
 private Integer residualDay;// 收费剩余时间(天);
 private Date publishtime;// 发表时间
 private Integer confineday;// 关屋天数
 private Date punishtime;// 处罚时间
 private Integer reportCount;// 举报次数
 private Date reportToResume; // 恢复发表时间;
 private String confinereason;// 关屋原因
 private String deductionreason;// 扣除积分
 private String adjustreason;// 调整点数
 private boolean deduction;//是否扣积分
 private boolean confine;//是不关小黑屋
 private boolean adjust;//是否积分调整

 

 public SimpleInformation() {

 }

 // 构造器
 public SimpleInformation(Integer infoid, Integer punishid, String subject,
   String result, Integer complaintresult, Integer readcount,
   Date publishtime, String confinereason, String deductionreason,
   String adjustreason,boolean deduction,boolean confine,boolean adjust) {
  this.infoid = infoid;
  this.punishid = punishid;
  this.subject = subject;
  this.result = result;
  this.complaintresult = complaintresult;
  this.readcount = readcount;
  this.publishtime = publishtime;
  this.confinereason = confinereason;
  this.deductionreason = deductionreason;
  this.adjustreason = adjustreason;
  this.deduction = deduction;
  this.confine = confine;
  this.adjust = adjust;
 }


 public String getConfinereason() {
  return confinereason;
 }

 public void setConfinereason(String confinereason) {
  this.confinereason = confinereason;
 }

 public Date getReportToResume() {
  return reportToResume;
 }

 public void setReportToResume(Date reportToResume) {
  this.reportToResume = reportToResume;
 }

 public Integer getReportCount() {
  return reportCount;
 }

 public void setReportCount(Integer reportCount) {
  this.reportCount = reportCount;
 }

 public Date getPunishtime() {
  return punishtime;
 }

 public void setPunishtime(Date punishtime) {
  this.punishtime = punishtime;
 }

 public Integer getConfineday() {
  return confineday;
 }

 public void setConfineday(Integer confineday) {
  this.confineday = confineday;
 }

 public Integer getInfoid() {
  return infoid;
 }

 public void setInfoid(Integer infoid) {
  this.infoid = infoid;
 }

 public Integer getPunishid() {
  return punishid;
 }

 public void setPunishid(Integer punishid) {
  this.punishid = punishid;
 }

 public String getSubject() {
  return subject;
 }

 public void setSubject(String subject) {
  this.subject = subject;
 }

 public String getReason() {
  return reason;
 }

 public void setReason(String reason) {
  this.reason = reason;
 }

 public String getResult() {
  return result;
 }

 public void setResult(String result) {
  this.result = result;
 }

 public Integer getComplaintresult() {
  return complaintresult;
 }

 public void setComplaintresult(Integer complaintresult) {
  this.complaintresult = complaintresult;
 }

 public Integer getReadcount() {
  return readcount;
 }

 public void setReadcount(Integer readcount) {
  this.readcount = readcount;
 }

 public Integer getCommentcount() {
  return commentcount;
 }

 public void setCommentcount(Integer commentcount) {
  this.commentcount = commentcount;
 }

 public Integer getScorenumber() {
  return scorenumber;
 }

 public void setScorenumber(Integer scorenumber) {
  this.scorenumber = scorenumber;
 }

 public Integer getResidualDay() {
  return residualDay;
 }

 public void setResidualDay(Integer residualDay) {
  this.residualDay = residualDay;
 }

 public Date getPublishtime() {
  return publishtime;
 }

 public void setPublishtime(Date publishtime) {
  this.publishtime = publishtime;
 }

 public String getDeductionreason() {
  return deductionreason;
 }

 public void setDeductionreason(String deductionreason) {
  this.deductionreason = deductionreason;
 }

 public String getAdjustreason() {
  return adjustreason;
 }

 public void setAdjustreason(String adjustreason) {
  this.adjustreason = adjustreason;
 }
 
 public boolean isDeduction() {
  return deduction;
 }

 public void setDeduction(boolean deduction) {
  this.deduction = deduction;
 }

 public boolean isConfine() {
  return confine;
 }

 public void setConfine(boolean confine) {
  this.confine = confine;
 }

 public boolean isAdjust() {
  return adjust;
 }

 public void setAdjust(boolean adjust) {
  this.adjust = adjust;
 }
}

 

DAO代码 调用构造器得到  list

@Transactional
public class InformationDBImpl implements InformationDB{

@SuppressWarnings("unchecked")
 public List<SimpleInformation> findByPunish(String type, String userid,
   Pagination pagination) {

  List list = new ArrayList();

  Query query1 = em
    .createQuery("select count(m) from Information m,Punish p where m.infoid = p.infoid  and m.type =:type and m.author.id =:userid");
  query1.setParameter("type", type);
  query1.setParameter("userid", userid);
  Long count = (Long) query1.getSingleResult();//分页统计
  pagination.setTotal(count.intValue());


  //运用构造器
  String queryPunishfree = " select new com.business.SimpleInformation(m.infoid ,p.punishid,m.subject,p.result,p.complaintresult,m.readcount,m.publishtime,p.confinereason,p.deductionreason,p.adjustreason,p.confine,p.deduction,p.adjust)"
    + " from Information m,Punish p where m.infoid = p.infoid "
    + " and m.type =:type and m.author.id =:userid";
  try {
   if (type.trim().equals("0")) {

    Query query = em.createQuery(queryPunishfree);// 收费查询
    query.setParameter("type", type);
    query.setParameter("userid", userid);

    query.setFirstResult(pagination.getStartIndex());//分页
    query.setMaxResults(pagination.getSize());

    list = query.getResultList();
    System.out.println("========queryPunishfree0 ="
      + queryPunishfree);
   } else {
    Query query = em.createQuery(queryPunishfree);// 免费查询
    query.setParameter("type", type);
    query.setParameter("userid", userid);

    query.setFirstResult(pagination.getStartIndex());
    query.setMaxResults(pagination.getSize());

    list = query.getResultList();
    System.out.println("========queryPunishfree1 ="
      + queryPunishfree);
   }
  } catch (RuntimeException re) {
   throw re;
  }
  return list;
 }
}


service  代码:

public List<SimpleInformation> findByPunish(String type, String userid,
   Pagination pagination) {

  return informationDB.findByPunish(type, userid, pagination);
 }

 

 

action  代码:
public class InformationAction extends ActionSupport
private List<SimpleInformation> simpleinfos;

this.simpleinfos = service.findByPunish(type, userid, pagination); //得到构造器list
   setPager(pagination.makePageList());  //分页设置
   result = "success"; // 处罚  --struts.xml result="success";

   }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics