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 12 Answers
Reset to default 0Modify 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)
.