You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot+Hibernate多对一JPQL查询报错:Entity未映射

多对一关联JPQL查询报错排查

我在编写多对一关联的自定义JPQL查询时遇到问题,MySQL原生查询可以正常运行,但JPQL查询抛出异常。

实体类代码

Opinions实体类

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Table(name = "Opinions")
public class OpinionsModel implements Serializable {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    private Long Opinie_id;

    @ManyToOne
    @JoinColumn(name="productId")
    private ProductModel productModel;

    private String Comment;

    private Double Rate;
}

Product实体类

@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "Product")
public class ProductModel{

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    private Long productId;

    private String productName;
    private String short_Description;
    private String long_Description;
    private Boolean slider;
    private Boolean discount;
    private double old_Price;
    private double new_Price;
    private String image;
}

仓库接口代码

@Repository
public interface OpinionsRepository extends JpaRepository<OpinionsModel,Long> {

    @Query("SELECT c FROM Opinions c WHERE c.ProductModel.productId = ?1")
    List<OpinionsModel> getOpinionToProduct();

}

抛出的异常

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Opinions is not mapped

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Opinions is not mapped [SELECT c FROM Opinions c WHERE c.ProductModel.productId = ?1]

可用的原生查询(正常运行)

@Query(value = "select * from opinions where opinions.product_id = :1", nativeQuery = true)
List<OpinionsModel> getOpinionToProduct();

正常工作的非外键JPQL查询示例

@Query("select p from ProductModel p WHERE p.slider = :state")
List<ProductModel> productOnSlider(Boolean state);

@Query("select p from ProductModel p WHERE p.slider = :sliderState AND p.discount = :DiscountState")
List<ProductModel> productOnDiscount(Boolean sliderState,Boolean DiscountState);

问题原因及修正方案

你的JPQL查询存在三个关键错误:

  1. 实体类名称引用错误
    JPQL是面向实体类的查询,必须使用实体类的类名而非数据库表名。你的实体类名为OpinionsModel,但查询里写了数据库表名Opinions,Hibernate无法找到对应的映射实体,因此抛出"Opinions is not mapped"异常。

  2. 实体属性大小写不匹配
    OpinionsModel中关联Product的属性是productModel(小写开头),但查询里写了c.ProductModel(大写P),JPQL对属性名大小写敏感,会导致无法识别该关联属性。

  3. 方法缺少参数
    查询中用?1声明了第一个参数,但getOpinionToProduct()方法没有定义任何参数,会导致参数绑定失败。

修正后的仓库接口代码

@Repository
public interface OpinionsRepository extends JpaRepository<OpinionsModel,Long> {

    @Query("SELECT c FROM OpinionsModel c WHERE c.productModel.productId = ?1")
    List<OpinionsModel> getOpinionToProduct(Long productId);

}

可选优化:使用命名参数

如果想让代码可读性更强,也可以改用命名参数的写法:

@Query("SELECT c FROM OpinionsModel c WHERE c.productModel.productId = :productId")
List<OpinionsModel> getOpinionToProduct(@Param("productId") Long productId);

内容的提问来源于stack exchange,提问作者asdasdas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 22:20:32