我在Google App Engine中有一对多的关系。即时通信使用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 Gift {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
@ManyToOne(fetch=FetchType.LAZY, targetEntity=Profile.class)
private Profile user = null;
...
}如何对子实体“Gift”进行分页?假设我必须首先退还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;
}发布于 2013-04-26 10:27:13
尝尝这个
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;
}通常,我在控制器和视图层(使用jstl的jsp)中进行分页。但也许您的需求需要在DAO层中执行此操作。
https://stackoverflow.com/questions/14948066
复制相似问题