悠悠 发表于 2009-2-12 09:00:30

JPA中的集合查询示例

1、VisitInfo模型
    view plaincopy to clipboardprint?
    @Entity
    @Table(name = "System_VisitInfo")
    @Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    public class VisitInfo implements Serializable {
      private static final long serialVersionUID = 2877662856939429897L;
   
      @Id
      @GeneratedValue(strategy = GenerationType.TABLE)
      private Long id;
   
      private Date vdate;
   
      @Column(length = 200)
      private String fromUrl;
   
      @Column(length = 200)
      private String url;
   
      @Column(length = 50)
      private String ip;
   
      private Integer status;
   
      @Column(length=1000)
      private String remark;
   
      @ManyToMany
      private List<SystemVisitDir> dirs=new java.util.ArrayList<SystemVisitDir>();
      public Long getId() {
            return id;
      }
    //setter及getter
    }
    @Entity
    @Table(name = "System_VisitInfo")
    @Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    public class VisitInfo implements Serializable {
   private static final long serialVersionUID = 2877662856939429897L;
   
   @Id
   @GeneratedValue(strategy = GenerationType.TABLE)
   private Long id;
   
   private Date vdate;
   
   @Column(length = 200)
   private String fromUrl;
   
   @Column(length = 200)
   private String url;
   
   @Column(length = 50)
   private String ip;
   
   private Integer status;
   
   @Column(length=1000)
   private String remark;
   
   @ManyToMany
   private List<SystemVisitDir> dirs=new java.util.ArrayList<SystemVisitDir>();
   public Long getId() {
      return id;
   }
    //setter及getter
    }view plaincopy to clipboardprint?
    <STRONG>2、SystemVisitDir 模型</STRONG>
   
   
                  
   2、SystemVisitDir 模型view plaincopy to clipboardprint?
    <PRE class=java name="code">
    @Entity
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    @FormPO(inject = "title,url,parent,sequence")
    public class SystemVisitDir implements Serializable, IJsonObject {
      private static final long serialVersionUID = -6636698603262819988L;
      @Id
      @javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.TABLE)
      private Long id;
   
      @Column(length = 20)
      @Field(validators = { @Validator(name = "string", value = "blank;min:2;max:20", required = true) })
      private String title;// 栏目名称
   
      @Column(length =500)
      @Field(validators = { @Validator(name = "url", required = true) })
      private String url;// 栏目url
   
      private Long num = 0l;// 访问量
   
      private Date lastVisitTime;// 最后一次访问时间
   
      private Integer sequence = 1;// 排序
   
      @javax.persistence.ManyToOne(fetch = FetchType.EAGER)
      @POLoad(name = "parentId")
      private SystemVisitDir parent;// 父级目录
   
      private Integer status = 0;
   
      @OneToMany(mappedBy = "parent")
      @OrderBy("sequence")
      private List<SystemVisitDir> children = new java.util.ArrayList<SystemVisitDir>();// 子目录
   
    //getter及setter
    }
    </PRE>
    view plaincopy to clipboardprint?@Entity    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
   @FormPO(inject = "title,url,parent,sequence")
   public class SystemVisitDir implements Serializable, IJsonObject
{
         private static final long serialVersionUID = -6636698603262819988L;
         @Id
      @javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.TABLE)
         private Long id;
             @Column(length = 20)
         @Field(validators = { @Validator(name = "string", value = "blank;min:2;max:20", required = true) })
         private String title;// 栏目名称
             @Column(length =500)
         @Field(validators = { @Validator(name = "url", required = true) })
         private String url;// 栏目url
             private Long num = 0l;// 访问量
             private Date lastVisitTime;// 最后一次访问时间
             private Integer sequence = 1;// 排序
             @javax.persistence.ManyToOne(fetch = FetchType.EAGER)
         @POLoad(name = "parentId")
         private SystemVisitDir parent;// 父级目录
             private Integer status = 0;
             @OneToMany(mappedBy = "parent")
         @OrderBy("sequence")
         private List<SystemVisitDir> children = new java.util.ArrayList<SystemVisitDir>();// 子目录
         //getter及setter
   }

上一页    
                  
    @Entity
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    @FormPO(inject = "title,url,parent,sequence")
    public class SystemVisitDir implements Serializable, IJsonObject {
   private static final long serialVersionUID = -6636698603262819988L;
   @Id
   @javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.TABLE)
   private Long id;
   
   @Column(length = 20)
   @Field(validators = { @Validator(name = "string", value = "blank;min:2;max:20", required = true) })
   private String title;// 栏目名称
   
   @Column(length =500)
   @Field(validators = { @Validator(name = "url", required = true) })
   private String url;// 栏目url
   
   private Long num = 0l;// 访问量
   
   private Date lastVisitTime;// 最后一次访问时间
   
   private Integer sequence = 1;// 排序
   
   @javax.persistence.ManyToOne(fetch = FetchType.EAGER)
   @POLoad(name = "parentId")
   private SystemVisitDir parent;// 父级目录
   
   private Integer status = 0;
   
   @OneToMany(mappedBy = "parent")
   @OrderBy("sequence")
   private List<SystemVisitDir> children = new java.util.ArrayList<SystemVisitDir>();// 子目录
   
    //getter及setter
    }
   
   
    3、集合查询
    集合查询主要涉及到一些关键字,主要是EMPTY及MEMBER OF。下面的例子查询某一个目录里面的VisitInfo
    public Page doListVisit(WebForm form, Module module)
{
   QueryObject qo = form.toPo(QueryObject.class);
   String id = CommUtil.null2String(form.get("parentId"));
   if (!"".equals(id))
{
    SystemVisitDir parent = this.service.getVisitDir(new Long(id));
    qo.addQuery("(?MEMBER OF obj.dirs)",new Object[]{parent});
   }
   IPageList pageList = service.queryVisistInfo(qo);
   form.jsonResult(pageList);   return Page.JSONPage;
}
   上一页
页: [1]
查看完整版本: JPA中的集合查询示例