I am trying to obtain a non-entity object, using a native query, which I'd like to use as a model for a view on one of the pages in my application. I am following this explanation which doesn't go in much detail, it is more like a cheat-sheet for someone who is already familiar with the topic and only needs a reminder. There are similar questions here and here from ages ago and since I can’t figure out where exactly the provided code blocks should go I tought I might ask a fresh question. What ever I try I get this exception:
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type
Here is my model class:
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;
}
The Repository interface:
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();
}
And one of the entity classes where I try to map the non-entity class:
@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...
}
I appreciate any input!