注册
 找回密码
 注册
江西广告网
查看: 333|回复: 0
打印 上一主题 下一主题

JPA中的集合查询示例

[复制链接]

该用户从未签到

1
跳转到指定楼层
发表于 2009-2-12 09:00:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?注册

x
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> [1] [2] [3] 2、SystemVisitDir 模型view plaincopy to clipboardprint? <RE 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 } 上一页 [1] [2] [3] @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] [2] [3]
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表