gpt4 book ai didi

java - 无法从类型 [java.lang.Object[]] 转换为类型

转载 作者:可可西里 更新时间:2023-11-01 07:36:30 24 4
gpt4 key购买 nike

我有 spring 网络应用程序(JPA/Hibernate + MySQL)。我有两个 DAO 类。

CustomerDAO

@Entity
@Table(name = "customers")
public class Customer {

@Id
@Column(name = "customer_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "name", length = 50)
private String name;

@Column(name = "surname", length = 50)
private String surname;

@OneToMany(mappedBy = "customer")
private Set<Order> orders = new HashSet<>();
}

OrderDAO

@Entity
@Table(name = "orders")
public class Order {

@Id
@Column(name = "order_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "date")
private Date date;

@Digits(integer = 5, fraction = 2)
@Column(name = "amount")
private BigDecimal amount;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "customer_id")
private Customer customer;

@OneToMany(mappedBy = "order")
private Set<OrderDetail> ordersDetails = new HashSet<>();

我有一个从数据库中检索数据的类:

@Repository
public interface OrderDAO extends JpaRepository<Order, Long> {

@Query("select o.customer.surname, sum(o.amount) as s from Order as o group by o.customer")
List<Customer> findCustomersBySumOfAmount();

}

它给我这样的结果:

+---------+---------------+
| surname | sum of amount |
+---------+---------------+
|Bielecki | 141.49 |
|Bielen | 130.34 |
......

现在我想用这种方法从 DB 中“拆箱”数据 List<Customer> findCustomersBySumOfAmount()

我的 spring Controller 类中有这个方法:

 List<Customer> findCustomersBySumOfAmount = orderService.findCustomersBySumOfAmount();
model.addAttribute("findCustomersBySumOfAmount", findCustomersBySumOfAmount);

for(Customer c : findCustomersBySumOfAmount) {
String s = c.getSurname();
System.out.println(c);
}

我有错误:

Failed to convert from type [java.lang.Object[]] to type [com.twistezo.models.Customer] for value '{Bielecki, 141.49}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [com.twistezo.models.Customer]

我想这是因为我得到了 List<Object[]> .我知道我可以在 List<Object[]> 之间迭代我的数据,但也许有一些更简单的方法可以将数据直接检索到 <Customer> ?我是这方面的新手。从现在开始,我使用了类似 List<Customer> findAll() 的方法没有@Query 注释,我正在寻找类似的“拆箱”。

我试图做这样的事情(在查询中添加 Customer.class)但没有效果:

@Query("select o.customer.surname, sum(o.amount) as s from Order as o group by o.customer", Customer.class)
List<Customer> findCustomersBySumOfAmount();

最佳答案

我已经收到此错误消息。就我而言,我正在执行类似以下代码的操作:

@Repository
public interface RepositoryA extends CrudRepository<EntityA, Long> {

@Query(nativeQuery = true, value = " ...SQL_1... ")
List<EntityA> getListOfEntityA(...PARAMS_1...);

@Query(nativeQuery = true, value = " ...SQL_2... ")
List<EntityB> getListOfEntityB(...PARAMS_2...);

}

我使用“EntityA”对存储库接口(interface)进行参数化,并使用另一种方法返回使用“EntityB”进行参数化的其他类型。

在您的情况下,您使用“Order”参数化 JpaRepository,然后使用返回“Customer”列表的方法...也许这是导致此异常的原因。

关于java - 无法从类型 [java.lang.Object[]] 转换为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42446506/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com