我需要帮助解决与EclipseLink 2.5.x提供程序的关系/查询问题。
从ThreePhaseMotorInput到ValidationMessage的关系应该是单向的OneToMany,即每个电机可以有0..n消息,而在Java对象图中,ValidationMessage没有对ThreePhaseMotorInput的引用。
我收到一条错误,即JPA在通过ThreePhaseMotor访问时无法找到属于ValidationMessage类的属性。 (见下面的错误文本)
感谢您考虑我的问题!
查询
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.
ThreePhaseMotorInput 类
@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;
ValidationMessage 类
@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;
答案 0 :(得分:4)
问题似乎出现在以下查询中:select m.approvedMotor, m.valMessages, m.valMessages.validationMsg, count(m.valMessages.id) from ThreePhaseMotorInput m group by m.valMessages.validationMsg
。
该查询应该是JPQL查询,即您指定实体及其Java属性的查询。如果您想跳转到其他实体的属性,也必须使用JOIN
: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
答案 1 :(得分:1)
您不能将路径表达式与Collection值关联一起使用。
从计算到集合的路径表达式中组合路径表达式在语法上是非法的。
在您的查询中,m.valMessages
是非法的,因为它引用了ValidationMessages的集合。
另一方面,m.approvedMotor
是合法的,因为它是单值关联。
正如Andrei回应中所建议的那样,您需要修改查询以添加另一个路径表达式:
select msg.validationMsg, COUNT(m.id) from ThreePhaseMotorInput m JOIN m.valMessages msg GROUP BY msg.validationMsg
答案 2 :(得分:0)
如果要跳转到其他实体的属性,则应使用JOIN。尝试以下JPQL查询
select m, COUNT(msg) from ThreePhaseMotorInput AS m LEFT JOIN m.valMessages AS msg GROUP BY msg.validationMsg