JPAでの集合クエリーの例
3316 ワード
1、VisitInfoモデル
@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
}
2、SystemVisitDir
@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; }