Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa 如何确保@ManyToMany列表的顺序?_Jpa_Jpa 2.0_Eclipselink - Fatal编程技术网

Jpa 如何确保@ManyToMany列表的顺序?

Jpa 如何确保@ManyToMany列表的顺序?,jpa,jpa-2.0,eclipselink,Jpa,Jpa 2.0,Eclipselink,考虑: @Entity public class M { @ManyToMany private List<E> es = new ArrayList<E>(); private E newE; public M(E newE) { this.newE = newE; es.add(newE); } 然而,这种繁重的代码甚至效率低下,因为它强制在实际需要之前从PU加载实体。有没有可行的替代方案

考虑:

@Entity
public class M {

    @ManyToMany
    private List<E> es = new ArrayList<E>();
    private E newE;

    public M(E newE) {
        this.newE = newE;
        es.add(newE);
    }
然而,这种繁重的代码甚至效率低下,因为它强制在实际需要之前从PU加载实体。有没有可行的替代方案

我不能断言(m.newE==m.getEs(0))

,如果在
@manytomy
中有任何对象,则将在列表末尾添加
newE
。另外,我相信在插入之前列表将被延迟加载,因此您对效率低下的担忧并不真正适用

如果您只关心从持久性单元加载时元素的顺序,那么我们可以做到这一点

@ManyToMany
@Orderby("someproperty ASC")
private List<E> es = new ArrayList<E>();
@manytomy
@订购人(“someproperty ASC”)
私有列表es=新的ArrayList();

如果您还希望在运行时添加元素时保持顺序,则必须实现或使用单独的、然后使用或使用自然排序的集合,如。

JPA
还提供了一个
@OrderColumn
,如果您希望保持顺序

否则,对列表元素顺序的更改不会被视为持久性更改

@ManyToMany
@Orderby("someproperty ASC")
private List<E> es = new ArrayList<E>();