【发布时间】:2021-10-09 17:39:14
【问题描述】:
我在数据库中有两个表(CAR_BODY 和 MOTOR)。我正在编写 hql 查询请求。我需要从此表中获取 3 列并创建 DTO 对象。
表: 车体 | ----------------| 身份证 | 重量 | NUMBER_OF_DOORS | SPARE_WHEEL |
| MOTOR |
|---|
| ID |
| ENGINE_DISPLACEMENT |
| TORQUE |
| NUMBER_OF_CYLINDERS |
DTO 对象:
public class CarInf{
private float weight;
private float engineDisplacement;
private float torque;
public CarInf(float weight, float engineDisplacement, float torque) {
this.weight = weight;
this.engineDisplacement = engineDisplacement;
this.torque = torque;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public float getEngineDisplacement() {
return engineDisplacement;
}
public void setEngineDisplacement(float engineDisplacement) {
this.engineDisplacement = engineDisplacement;
}
public float getTorque() {
return torque;
}
public void setTorque(float torque) {
this.torque = torque;
}
}
这是我在存储库界面中的方法:
@Query(nativeQuery = true, value = "select new CarInf(cb.weight, m.engineDisplacement, m.torque) from Motor as m join CarBody as cb on m.id = cb.id where m.id = ?1")
public List<CarInf> getCarInf(Long id);
但我得到 org.hibernate.exception.SQLGrammarException: could not extract ResultSet。
我的错误是什么?
【问题讨论】:
-
当你使用
nativeQuery = true你的查询变成了真正的SQL。 HQL 不是 nativeQuery。 -
还将
?1更改为:id。