Java Google App Engine-JPA中一对多关系的分页
我在谷歌应用程序引擎中有一对多的关系。我在用JPAJava Google App Engine-JPA中一对多关系的分页,java,google-app-engine,jpa,pagination,Java,Google App Engine,Jpa,Pagination,我在谷歌应用程序引擎中有一对多的关系。我在用JPA public class Profile { @OneToMany(targetEntity=Gift.class, mappedBy="user", fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.REMOVE}) @OrderBy("date DESC") private List<Gift> gift = null;
public class Profile {
@OneToMany(targetEntity=Gift.class, mappedBy="user", fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.REMOVE})
@OrderBy("date DESC")
private List<Gift> gift = null;
...
}
public class Gift {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
@ManyToOne(fetch=FetchType.LAZY, targetEntity=Profile.class)
private Profile user = null;
...
}
公共类配置文件{
@OneToMany(targetEntity=Gift.class,mappedBy=“user”,fetch=FetchType.LAZY,cascade={CascadeType.PERSIST,CascadeType.REMOVE})
@订购人(“日期描述”)
私人列表礼品=空;
...
}
公共类礼物{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私钥;
@manytone(fetch=FetchType.LAZY,targetEntity=Profile.class)
私有配置文件用户=null;
...
}
如何对子实体“礼物”进行分页?比如说,我必须先归还第1-10份礼物,然后再归还第11-20份礼物
目前,我返回了整个列表
public List<Gift> listGift(String email) throws PersistenceException{
EntityManager em = EMF.get().createEntityManager();
EntityTransaction tx = null;
List<Gift> list = null;
try{
tx = em.getTransaction();
tx.begin();
Profile user = em.find(Profile.class, email);
list = new ArrayList<Gift>(user.getGift());
tx.commit();
}finally{
try {
if (tx != null && tx.isActive()) {
tx.rollback();
}
} finally {
em.close();
}
}
return list;
}
公共列表listGift(字符串电子邮件)引发PersistenceException{
EntityManager em=EMF.get().createEntityManager();
EntityTransaction tx=null;
List=null;
试一试{
tx=em.getTransaction();
tx.begin();
Profile user=em.find(Profile.class,电子邮件);
list=newarraylist(user.getGift());
tx.commit();
}最后{
试一试{
如果(tx!=null&&tx.isActive()){
tx.回滚();
}
}最后{
em.close();
}
}
退货清单;
}
试试这个
public List<Gift> listGift(String email,int p) throws PersistenceException{
EntityManager em = EMF.get().createEntityManager();
EntityTransaction tx = null;
List<Gift> list = null;
List pagedList = new ArrayList();
int view=10;
try{
tx = em.getTransaction();
tx.begin();
Profile user = em.find(Profile.class, email);
list = new ArrayList<Gift>(user.getGift());
int begin=(p-1)*view;
int end = p*view;
if(end> list.size())
end=list.size();
for (int i=begin;i<end;i++){
pagedList.add(list.get(i));
}
tx.commit();
}finally{
try {
if (tx != null && tx.isActive()) {
tx.rollback();
}
} finally {
em.close();
}
}
return pagedList;
}
公共列表listGift(字符串电子邮件,int p)引发PersistenceException{
EntityManager em=EMF.get().createEntityManager();
EntityTransaction tx=null;
List=null;
List pagedList=new ArrayList();
int view=10;
试一试{
tx=em.getTransaction();
tx.begin();
Profile user=em.find(Profile.class,电子邮件);
list=newarraylist(user.getGift());
int begin=(p-1)*视图;
int end=p*视图;
if(end>list.size())
end=list.size();
for(int i=开始;i