Home > database >  Failed to convert from type (Java - Spring boot)
Failed to convert from type (Java - Spring boot)

Time:12-12

I have spring web app (JPA/Hibernate MySQL). I have three classes.

Top10

    public class Top10 {
    @Id
    Product id;
    Product name;
    Integer soluong;
    }

Product

    public class Product implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;
    @Column(name = "name")
    String name;
    @Column(name = "image")
    String image;
    @Column(name = "count")
    Integer count;
    @Column(name = "special")
    Boolean special;
    @Column(name = "price")
    Double price;
    @Column(name = "description")
    String description;
    @ManyToOne
    @JoinColumn(name = "category_id", referencedColumnName = "id")
    Category categoryId;
    @Column(name = "create_date")
    Date createdate = new Date();
    @ManyToOne
    @JoinColumn(name = "product_status_id")
    ProductStatus productStatus;
    
    @JsonIgnore
    @OneToMany(mappedBy = "product")
    List<OrderDetail> orderDetails;
    }

ProductDetail

    public class OrderDetail implements Serializable  {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;
    Double price;
    Integer quantity;
    @ManyToOne
    @JoinColumn(name = "Productid")
    Product product;
    @ManyToOne
    @JoinColumn(name = "Orderid")
    Order order;
    }

And I have a ProductDao class for retrieving data from DB:

    @Repository
    public interface ProductDAO extends JpaRepository<Product, Long> {
    
    @Query(value ="select top(10) od.ProductId,p.name , sum(Quantity) as 
    Quantity\r\n"
                  "from Products p, order_details od\r\n"
                  "where p.id = od.ProductId\r\n"
                  "group by p.name, od.ProductId\r\n"
                  "order by Quantity desc",nativeQuery =true)
        List<Top10> top10Product();
}

And one class interface :

ProductInterface

      public interface productService {
        
        List<Top10> top10product();
       }

ProductInterfaceImpl

     @Service
       public class productServiceImpl implements productService{
        @Autowired
        ProductDAO dao;
    
        @Override
        public List<Top10> top10product() {
            // TODO Auto-generated method stub
            return dao.top10Product();
        }

       }

[This result like this][1]

Now i want 'unbox' data from DB with this method List getAll()

I have a method for this in my spring controller class:

    @CrossOrigin("*")
    @RestController
    @RequestMapping("/rest/chart")
    public class chartRestController {
        @Autowired
        productService productService;
        
        @GetMapping()
        public List<Top10> getAll(){
            return productService.top10product();
        }
        
    }

And i have error:

There was an unexpected error (type=Internal Server Error, status=500). Failed to convert from type [java.lang.Object[]] to type [com.gymshop.domain.Top10] for value '{8, ISOJECT Premium EVOGEN - Whey Isolate, 6}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Integer] to type [com.gymshop.domain.Top10] org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type [com.gymshop.domain.Top10] for value '{8, ISOJECT Premium EVOGEN - Whey Isolate, 6}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Integer] to type [com.gymshop.domain.Top10] at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47) at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:192) at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:175) at org.springframework.data.repository.query.ResultProcessor$ProjectingConverter.convert(ResultProcessor.java:313) at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.lambda$and$0(ResultProcessor.java:229) at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.convert(ResultProcessor.java:240) at org.springframework.data.repository.query.ResultProcessor.processResult(ResultProcessor.java:156) at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:158) at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:143)

CodePudding user response:

Shouldn't your Top10 class look as follows?

public class Top10 {
    Long id;
    Product name;
    String name;
}

CodePudding user response:

to define a entity (table) do you need to use the spring tags (@Entity)

@Entity
@Table(name = "Top10")
public class Top10 {
    @Id
    Product id;
    Product name;
    Integer soluong;
    }
  • Related