Java Google应用程序引擎中的有序列表-JPA

Java Google应用程序引擎中的有序列表-JPA,java,google-app-engine,list,jpa,Java,Google App Engine,List,Jpa,漫画对象可以有多个章节对象 我在漫画课上有这个: @OneToMany(targetEntity=Chapter.class, mappedBy="comics", fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.REMOVE}) private List<Chapter> chapters = null; 问题是read方法返回了章节的意外顺序。按顺序显示章节的最佳方法是什么?您可以使用@OrderB

漫画对象可以有多个章节对象

我在
漫画
课上有这个:

@OneToMany(targetEntity=Chapter.class, mappedBy="comics", fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.REMOVE})
private List<Chapter> chapters = null;

问题是
read
方法返回了
章节的意外顺序。按顺序显示
章节的最佳方法是什么?

您可以使用@OrderBy注释:

例如:

@OneToMany(targetEntity=Chapter.class, mappedBy="comics", fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.REMOVE})
@OrderBy("chapterNumber")
private List<Chapter> chapters = null;
@OneToMany(targetEntity=Chapter.class,mappedBy=“comics”,fetch=FetchType.LAZY,cascade={CascadeType.PERSIST,CascadeType.REMOVE})
@订购人(“chapterNumber”)
私有列表章节=空;

这是假设有一个可比较的chapterNumber字段

如何移动和删除列表中的元素?我成功地使用chapterNumber移动了。但在删除时,即使我为其余实体设置了正确的chapterNumber,fetch也无法正确检索其余实体。
public Comics read(String key) throws IllegalAccessException, InvocationTargetException{
    EntityManager em = EMF.get().createEntityManager();
    Comics comics = new Comics();
    try{
        Comics emComics = em.find(Comics.class, KeyFactory.stringToKey(key));
        BeanUtils.copyProperties(comics, emComics);
        comics.setChapters(new LinkedList<Chapter> (emComics.getChapters()));

    }finally{
        em.close();
    }
    return comics;
}
comics.setChapters( new LinkedList<Chapter>() );
@OneToMany(targetEntity=Chapter.class, mappedBy="comics", fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.REMOVE})
@OrderBy("chapterNumber")
private List<Chapter> chapters = null;