最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

When the result of an SQL query is in a List, I get a Dto(product=test) Spring Boot Data Jpa - Stack Overflow

programmeradmin1浏览0评论

When querying SQLl, I get either the path to the DTO, or the text in the format - DTO(column=result)

This is the situation with an SQL query via Data Jpa - @NativeQuery(value = "SELECT o.product FROM orders o") List<TableProduct> findAllProduct(); Dto:

public class TableProduct {

    private String product;

public TableProduct() { }

    public String getProduct() {
        return product;
    }
    public void setProduct(String product) {
    this.product = product;
    }


public TableProduct(String product) {
    this.product = product;
}

@Entity
@Data
@Table(name = "orders")

public class Order {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "order_id")
    private Long orderId;
    @Column(name = "product")
    private String product;

    private String customerName;

    private String executorName;

    private String address;

    private OffsetDateTime offsetDateTime;

    private BigDecimal purchasesPrice;

    private BigDecimal purchasesSell;

    private String soldStatus;
}

I get the result: .example.botservice.dto.TableProduct@13bc40cf I tried with the @Data annotation, and it gave me: TableProduct(product=test That is, DTO(column=result)

And I need to get only "result".

When querying SQLl, I get either the path to the DTO, or the text in the format - DTO(column=result)

This is the situation with an SQL query via Data Jpa - @NativeQuery(value = "SELECT o.product FROM orders o") List<TableProduct> findAllProduct(); Dto:

public class TableProduct {

    private String product;

public TableProduct() { }

    public String getProduct() {
        return product;
    }
    public void setProduct(String product) {
    this.product = product;
    }


public TableProduct(String product) {
    this.product = product;
}

@Entity
@Data
@Table(name = "orders")

public class Order {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "order_id")
    private Long orderId;
    @Column(name = "product")
    private String product;

    private String customerName;

    private String executorName;

    private String address;

    private OffsetDateTime offsetDateTime;

    private BigDecimal purchasesPrice;

    private BigDecimal purchasesSell;

    private String soldStatus;
}

I get the result: .example.botservice.dto.TableProduct@13bc40cf I tried with the @Data annotation, and it gave me: TableProduct(product=test That is, DTO(column=result)

And I need to get only "result".

Share Improve this question asked Mar 12 at 9:43 Zaraza-itZaraza-it 1
Add a comment  | 

2 Answers 2

Reset to default 0

Modify your repository method as follows if you simply require product names.

@Query(value = "SELECT o.product FROM orders o", nativeQuery = true)
List<String> findAllProduct();

1. Customize the toString() Method in Your DTO Class

If you want a more readable output, override the toString() method in your Dto class. For example:

java

Copy

public class Dto {
    private String product;

    // Constructor, getters, and setters

    @Override
    public String toString() {
        return "Product: " + product;
    }
}

Now, when you print the List<Dto>, it will show Product: test instead of Dto(product=test).

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论