我有 Spring Web 應用程式(JPA/Hibernate MySQL)。我有三個班級。
前10名
public class Top10 {
@Id
Product id;
Product name;
Integer soluong;
}
產品
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;
}
產品資訊
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;
}
我有一個 ProductDao 類用于從資料庫檢索資料:
@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();
}
和一類介面:
產品界面
public interface productService {
List<Top10> top10product();
}
產品介面Impl
@Service
public class productServiceImpl implements productService{
@Autowired
ProductDAO dao;
@Override
public List<Top10> top10product() {
// TODO Auto-generated method stub
return dao.top10Product();
}
}
[這樣的結果][1]
現在我想用這個方法從 DB 中“拆箱”資料 List getAll()
我的 spring 控制器類中有一個方法:
@CrossOrigin("*")
@RestController
@RequestMapping("/rest/chart")
public class chartRestController {
@Autowired
productService productService;
@GetMapping()
public List<Top10> getAll(){
return productService.top10product();
}
}
我有錯誤:
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)
uj5u.com熱心網友回復:
你的Top10班級不應該是這樣的嗎?
public class Top10 {
Long id;
Product name;
String name;
}
uj5u.com熱心網友回復:
定義物體(表)是否需要使用彈簧標簽(@Entity)
@Entity
@Table(name = "Top10")
public class Top10 {
@Id
Product id;
Product name;
Integer soluong;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/382050.html
