在使用EclipseLink 2.5.x提供程序解决关系/查询问题时,我需要帮助。
从ThreePhaseMotorInput到ValidationMessage的关系应该是单向的OneToMany,即每个电动机可以具有0..n消息,并且在Java对象图中ValidationMessage没有对ThreePhaseMotorInput的引用。
我收到一个错误,当通过ThreePhaseMotor访问JPA时,它找不到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;
最佳答案
问题似乎在以下查询中: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