I've a OneToOne relation between Participant and abstract entity. I want to query for Participant that has an abstract. So I create the following query:

@NamedQuery(name = Participant.FIND_ABSTRACT, query = "SELECT p FROM Participant p WHERE p.abstract_ IS NOT EMPTY ORDER BY p.email"),
....
....
@OneToOne
@JoinColumn
private Abstract abstract_;

From Abstract.java:

@OneToOne(mappedBy="abstract_")
private Participant participant;

When I try to use it:

public List<Participant> getUploadedAbstract() {
    TypedQuery<Participant> query = em.createNamedQuery(Participant.FIND_ABSTRACT, Participant.class);
    return query.getResultList();
}

I obtain:

Caused by: Exception [EclipseLink-6071] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.QueryException
Exception Description: Invalid use of anyOf() for a query key    [org.eclipse.persistence.mappings.OneToOneMapping[abstract_]] not representing a to-many    relationship in an expression.  Use get() rather than anyOf().
at org.eclipse.persistence.exceptions.QueryException.invalidUseOfAnyOfInExpression(QueryException.java:738)
at org.eclipse.persistence.internal.expressions.QueryKeyExpression.validateNode(QueryKeyExpression.java:894)
at org.eclipse.persistence.expressions.Expression.normalize(Expression.java:2985)
at org.eclipse.persistence.internal.expressions.DataExpression.normalize(DataExpression.java:342)
at org.eclipse.persistence.internal.expressions.QueryKeyExpression.normalize(QueryKeyExpression.java:612)
at org.eclipse.persistence.internal.expressions.RelationExpression.normalize(RelationExpression.java:605)
at org.eclipse.persistence.internal.expressions.SQLSelectStatement.normalize(SQLSelectStatement.java:1300)
at org.eclipse.persistence.internal.expressions.SubSelectExpression.normalizeSubSelect(SubSelectExpression.java:197)
at org.eclipse.persistence.internal.expressions.ExpressionNormalizer.normalizeSubSelects(ExpressionNormalizer.java:93)

... ...

Where I wrong?

有帮助吗?

解决方案

You need to edit your named query.. It should be

@NamedQuery(name = Participant.FIND_ABSTRACT, query = "SELECT p FROM Participant p WHERE p.abstract_ IS NOT NULL ORDER BY p.email")

IS NOT EMPTY is used for collection.

其他提示

IS [NOT] EMPTY is used to select entities with a [not] empty collection of associated entities, i.e. on a OneToMany or ManyToMany association. You probably want

where p.abstract_ IS NOT NULL
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top