首页 > 解决方案 > 我可以将 hibernate/jpa 查询的结果映射到不是实体的类吗?

问题描述

我正在尝试使用 Springboot,我有几个具有几个非常相似的属性的实体,例如:实体“汽车”具有模型和价格属性,“摩托车”模型也是如此。我想使用一个类来定义那些常见的属性,即带有属性模型和价格的“车辆”类。

换句话说:

Car                             Motorcicle                             Vehicle
price                              price                                price
model                              model                                 model
year                              year
color                              color
fuel_type                         fuel_type
... 30 more properites                ...30 more properties

我想使用车辆来映射 Car 和 Motorcycle 表之间的 SQL 联合的搜索结果。

public interface ImovelRepositorio extends JpaRepository<Vehicle, Long> {

      @Query(value = "here select query with union between car and motorcycle", nativeQuery = true)
  List<Vehicle> searchVehicle();

}

这里的 Vehicle 类是一个没有任何注释的普通类。我收到错误消息说 Vehicle 不是托管类型。当然我的存储库没有意义,我预计会出现这个错误。但是我怎样才能实现我想要的呢?

当我执行联合时,我没有得到模型列表,而是得到一个或多个模型的列表。我想创建一个类来“组合”搜索结果。

标签: javahibernatespring-bootjpa

解决方案


FluentJPA 有针对这种情况的解决方案。


推荐阅读