问题
Motivated by this question, I've tried to change the following:
@ManyToMany(cascade={CascadeType.REMOVE, CascadeType.MERGE})
private Set<Expression> exps = new LinkedHashSet<Expression>();
to:
@ManyToMany(cascade={CascadeType.REMOVE, CascadeType.MERGE})
private LinkedHashSet<Expression> exps = new LinkedHashSet<Expression>();
However, such a move results in:
Exception [EclipseLink-1] (Eclipse Persistence Services - 2.1.0.v20100614-r7608): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The attribute [exps] is not declared as type ValueHolderInterface, but its mapping uses indirection. Mapping: org.eclipse.persistence.mappings.ManyToManyMapping[exps] Descriptor: RelationalDescriptor(com.mysimpatico.memoplatform.persistence.entities.Meaning --> [DatabaseTable(MEANING)])
How am I supposed in JPA2 to achieve the desired behavior?
回答1:
Only the standard interfaces types are allowed as attribute types in JPA. Why are you changing the type of the attribute? With the original definition the underlying collection is still your custom type.
回答2:
I believe EclipseLink will allow you to use a concrete collection type if you set fetch to EAGER. When the relationship is LAZY EclipseLink requires to put its own lazy collection in place of the value. EclipseLink defines a lazy List, Set and Map, but no LinkedHashSet.
Note the JPA spec requires the usage of the Collection, Set, List or Map interface, no impls are allowed. If order is important, you should just use a List.
Technically it should be possible to maintain the collection implementation with LAZY collections, so please log a bug/enhancement for this.
来源:https://stackoverflow.com/questions/3920531/why-cannot-a-jpa-mapping-attribute-be-a-linkedhashset