当前位置: 首页 > 知识库问答 >
问题:

有可能调用原生查询并将结果集存储在非实体对象中吗?

胡浩瀚
2023-03-14

我试图使用本机查询获得一个非实体对象,我想将其用作应用程序中一个页面上视图的模型。我遵循这个解释,它没有太多细节,它更像是一张备忘单,写给已经熟悉这个主题的人,只需要提醒。很多年前这里和这里都有类似的问题,因为我不知道所提供的代码块应该放在哪里,我想我可能会问一个新问题。无论我尝试什么,我都会遇到以下例外:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type

这是我的模型类:

public class ProductForHomeView {

    private int id;
    private String title;
    private EProductType productType;
    private double price;
    private int quantity;

    public ProductForHomeView(int id, String title, EProductType productType, double price, int quantity) {
        this.id = id;
        this.title = title;
        this.productType = productType;
        this.price = price;
        this.quantity = quantity;
    }

存储库界面:

public interface IProductsRepository extends CrudRepository<Products, Integer> {
    @Query(value = "SELECT products.id, products.title, products.the_type AS productType, stock.price, stock.quantity FROM products LEFT JOIN stock on products.id=stock.product_id", nativeQuery = true)
    List<ProductForHomeView> getProductsInfoForTheHomePage();   
}

以及我尝试映射非实体类的实体类之一:

@Entity
@Table(name = "stock")
@SqlResultSetMapping(
        name="ProductForHomeViewMapping",
        classes={
        @ConstructorResult(
        targetClass=qualified.name.ProductForHomeView.class,
        columns={
        @ColumnResult(name="id", type=Integer.class),
        @ColumnResult(name="title", type=String.class),
        @ColumnResult(name="productType", type=EProductType.class),
        @ColumnResult(name="price", type=Long.class),
        @ColumnResult(name="quantity", type=String.class)
        })})
public class Stock {
// mapped fields, constructors, geters and setters...
}

我感谢任何输入!

共有2个答案

宦瀚
2023-03-14

我使用了@SqlResultSetMapping@NamedNativeQuery

使用以下内容更改您的凝乳储存库方法

public interface IProductsRepository extends CrudRepository<Products, Integer> {
    @NamedNativeQuery(value = "SELECT products.id, products.title, products.the_type AS productType, stock.price, stock.quantity FROM products LEFT JOIN stock on products.id=stock.product_id", resultSetMapping = "ProductForHomeViewMapping")
    List<ProductForHomeView> getProductsInfoForTheHomePage();   
}

更多信息请点击此处

谢英光
2023-03-14

您可以将视图声明为一个接口,然后Spring Data将按照您想要的方式处理它。

以下是示例:

https://github.com/roberthunt/spring-data-native-query-projection/blob/master/src/main/java/uk/co/rbrt/PersonSummary.java

https://github.com/roberthunt/spring-data-native-query-projection/blob/master/src/main/java/uk/co/rbrt/PersonRepository.java

 类似资料:
  • JPA存储库方法是:@query(“select date,sum(crAmt),sum(drAmt)from Daybook u where u date=?1”) public DaybookBalance findDaybookBalance(字符串d1); 我得到以下错误

  • 如何将查询结果保存在字符串数组中 查询很简单,只有一列,即: 我想要的是将id存储在String数组中,这样我就可以将它们显示为列表视图中的可点击项。

  • 获取查询对象的结果集 // 查询邮箱或密码是否正确 async checkEmailAndPassword(loginData) { // 解构查询对象中的结果 const { dataValues: {id,username,password,email} } = await this.ctx.model.User.findOne({

  • 我有一个复杂的本机查询,我正在尝试将其结果映射到非实体DTO类。我正在尝试使用的with 我的DTO类 我的entity类,它具有我将调用此本机查询结果的存储库接口。 存储库 当我从ItemRepository调用getItemDetails()时,出现以下错误: org.springframework.data.mapping.属性引用异常:没有属性项找到项目类型的详细信息 使用和并解决此问题的

  • 问题内容: 当我使用变量执行此查询时,将显示此错误。 问题答案: 用括号括起来的选择。