我需要在JPA查询中加入表和视图 . 查询将无法编译,因为无法识别视图列 .
任何建议都非常感谢 .
Updated with parent entity and consistent naming
查询是:
select count(m.id)
from MultiSpeedMotor m,
MultiSpeedQuery q1
where m.id = q1.motorId
and q1.power = 10
错误是:
The state field path 'q1.motorId' cannot be resolved to a valid type.
The state field path 'q1.power' cannot be resolved to a valid type.
我正在使用遗留数据库,该数据库具有与此类似的非规范化表
Long motorId
Long id
Double hi_power
Double lo_power
我使用了一个带有union查询的视图来规范化这个表
Long motorId
Long id
Long hi
Double power
为了在JPA中建模联合查询的视图,我使用了@IdClass
public class MultiSpeedQueryId implements Serializable {
private static final long serialVersionUID = -7996931190943239257L;
private Long motorId;
private Long id;
private Long hi;
...
}
@Entity
@Table(name = "multi_speed_query")
@IdClass(MultiSpeedQueryId.class)
public class MultiSpeedQuery implements IMultiSpeedQuery {
@Id
@Column(name = "motor_id")
private Long motorId;
@Id
private Long id;
@Id
private Long hi;
private Double power;
...
}
父实体映射为:
@Entity
@Table(name = "multi_speed_motor")
public class MultiSpeedMotor implements Serializable, IMultiSpeedMotor {
private static final long serialVersionUID = 3019928176257499187L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
}
1 回答
查询是正确的写入 .
您可以使用语法加入没有预定义关系的实体 .
这个问题要简单得多 . 我错过了告诉真正问题的部分JPA错误日志 .
一旦我将实体添加到persistence.xml中,查询就像最初编写的那样完美 .