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 EclipseLink-6015表达式中的查询键[customer]无效_Jpa_Eclipselink_Jpa 2.1 - Fatal编程技术网

Jpa EclipseLink-6015表达式中的查询键[customer]无效

Jpa EclipseLink-6015表达式中的查询键[customer]无效,jpa,eclipselink,jpa-2.1,Jpa,Eclipselink,Jpa 2.1,我需要得到的是所有事件s,其中CustomerEvent.customer.id=123。我实际上得到的是例外 仅具有相关成员的简化实体: @Table(name = "EVENT") @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING, length = 1) p

我需要得到的是所有
事件
s,其中
CustomerEvent.customer.id=123
。我实际上得到的是例外

仅具有相关成员的简化实体:

@Table(name = "EVENT")
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING, length = 1)
public abstract class Event {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(nullable = false)
    private Integer id;
}

@Entity
@DiscriminatorValue("C")
public class CustomerEvent extends Event {
    @ManyToOne(optional = false)
    private Customer customer;
}

@Entity
@Table(name = "CUSTOMER")
public class Customer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(nullable = false)
    private Integer id;

}
查询:

final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Event> cq = cb.createQuery(Event.class);
final Root<Event> root = cq.from(Event.class);
cq.select(root);
cq.where(cb.equal(((Root<CustomerEvent>) root.as(CustomerEvent.class)).get(CustomerEvent_.customer).get(Customer_.id), 123));

// this predicate doesn't work either: cb.isNotNull(((Root<CustomerEvent>) root.as(CustomerEvent.class)).get(CustomerEvent_.customer));

em.createQuery(cq).getResultList(); // throws exception
如果您使用的是root.as(CustomerEvent.class),为什么不直接查询CustomerEvent呢?只有CustomerEvent实例可以具有CustomerEvent.customer.id=123,否则您不需要使用“as”函数

不推荐使用“As”,而应使用JPA的Treat谓词(包含在EclipseLink 2.5.1中)-区别在于Treat将排除该谓词中的非customerEvent实例,而“As”仅强制转换谓词,因此更难使用且不太稳定。Treat允许您安全地使用更复杂的表达式,例如
“从事件中选择事件(将事件作为CustomerEvent.customer.id=123)或事件.somethingelse=sometherCondition”

谢谢,
cb.treat()
修复了我的问题!为了回答您的问题,我不能直接使用
CustomerEvent
,原因有二:1
cq.from(Event.class)
是在父抽象类中构建的,我将其内联到testcase只是为了简化2。我在
cb.或(…)
中还有其他条件直接在
事件上操作-我遇到了中描述的问题。将Glassfish 4.0中的EclipseLink从bundled 2.5.0升级到2.5.1修复了它。
Exception [EclipseLink-6015] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.QueryException
Exception Description: Invalid query key [customer] in expression.
Query: ReadAllQuery(referenceClass=Event )
    at org.eclipse.persistence.exceptions.QueryException.invalidQueryKeyInExpression(QueryException.java:691)