JPA 状态字段路径无法解析为有效类型

JPA The state field path cannot be resolved to a valid type

我需要帮助解决与 EclipseLink 2 的关系/查询问题。5.x 提供商。

从 ThreePhaseMotorInput 到 ValidationMessage 的关系应该是单向的 OneToMany,即每个电机可以有 0..n 条消息,并且在 Java 对象图中 ValidationMessage 没有对 ThreePhaseMotorInput 的引用。

当通过 ThreePhaseMotor 访问时,我收到一个错误,指出 JPA 找不到作为 ValidationMessage class 一部分的属性。 (请参阅下面的错误文本)

感谢您考虑我的问题!

查询

select msg.validationMsg, COUNT(m.id) from ThreePhaseMotorInput AS m JOIN m.valMessages AS msg GROUP BY msg.validationMsg

错误

 org.eclipse.persistence.exceptions.JPQLException: 
Exception Description: Problem compiling [select msg.validationMsg, COUNT(m.id) from ThreePhaseMotorInput AS m JOIN m.valMessages AS msg GROUP BY msg.validationMsg]. 
[7, 24] The state field path 'msg.validationMsg' cannot be resolved to a valid type.
[71, 84] The collection-valued path 'm.valMessages' cannot be resolved to a valid association field.
[119, 136] The state field path 'msg.validationMsg' cannot be resolved to a valid type.

三相电机输入 class

@Entity
@Table(name = "three_phase_motor_input")
public class ThreePhaseMotorInput implements IThreePhaseMotorInput, Serializable {
    private static final long serialVersionUID = 8084370807289186987L;
    @Transient
    private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Version
    private Integer version;
    private Integer status;
    @Transient
    private Integer numMessages;

    @OneToOne(cascade = CascadeType.ALL, optional = true, targetEntity = UnapprovedThreePhaseMotor.class)
    @JoinColumn(name = "unapproved_id")
    private IThreePhaseMotor unapprovedMotor;

    @OneToOne(cascade = CascadeType.ALL, optional = true, targetEntity = ApprovedThreePhaseMotor.class)
    @JoinColumn(name = "approved_id")
    private IThreePhaseMotor approvedMotor;

    @OneToMany(orphanRemoval = true, cascade = CascadeType  .ALL, fetch = FetchType.LAZY, targetEntity = ValidationMessage.class)
    @JoinColumn(name = "input_id", referencedColumnName = "id", nullable = false)
    @OrderColumn(name = "idx")
    private List<IValidationMessage> valMessages;

验证消息 class

@Entity
@Table(name = "validation_message")
public class ValidationMessage implements Serializable, IValidationMessage {
    private static final long serialVersionUID = 8765213112015434057L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "record_id")
    private Long recordId;
    @Column(name = "field_name")
    private String fieldName;
    @Column(name = "validation_msg")
    private String validationMsg;
    private Integer status;
    @Column(name = "fail_field")
    private String failField;
    @Column(name = "error_source")
    private Integer errorSource;

问题似乎出在以下查询中:select m.approvedMotor, m.valMessages, m.valMessages.validationMsg, count(m.valMessages.id) from ThreePhaseMotorInput m group by m.valMessages.validationMsg

该查询应该是 JPQL 查询,即指定实体及其 Java 属性的查询。如果你想跳转到另一个实体的属性,你也必须使用 JOINs:m.valMessages.validationMsg 是不正确的,但是 INNER JOIN m.valMessages msg GROUP BY msg 是正确的。

所以请尝试以下查询:

select m, COUNT(msg) from ThreePhaseMotorInput AS m LEFT JOIN m.valMessages AS msg GROUP BY msg.validationMsg

不能将路径表达式与集合值关联一起使用。

文档说:JPQL Path Expressions

It is syntactically illegal to compose a path expression from a path expression that evaluates to a collection.

在您的查询中,m.valMessages 是非法的,因为它引用了一组 ValidationMessages。

另一方面,m.approvedMotor是合法的,因为它是一个单值关联。

根据 Andrei 回复中的建议,您需要修改查询以添加另一个路径表达式:

select msg.validationMsg, COUNT(m.id) from ThreePhaseMotorInput m JOIN m.valMessages msg GROUP BY msg.validationMsg

如果你想跳转到另一个实体的属性,你应该使用 JOIN。试试下面的 JPQL 查询

select m, COUNT(msg) from ThreePhaseMotorInput AS m LEFT JOIN m.valMessages AS msg GROUP BY msg.validationMsg