gpt4 book ai didi

无法转换 java.lang.Object

转载 作者:行者123 更新时间:2023-11-30 08:57:44 28 4
gpt4 key购买 nike

我正在尝试让所有用户对自定义问题中的消息投票:

List<Vote> list = sessionFactory.getCurrentSession()
.createQuery("from Vote as v left join v.message as m " +
"where m.question=:question and v.user=:user and v.voteType=:voteType")
.setParameter("question", question)
.setParameter("user", user)
.setParameter("voteType", VoteType.MESSAGE)
.list();
System.out.println(list.get(0).getMessage().getNumber());

最后一个字符串出现异常:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to ru.kapahgaiii.qa.domain.Vote ru.kapahgaiii.qa.repository.ChatDAOImpl.getVotes(ChatDAOImpl.java:114)

我做错了什么?

投票.java:

@Entity
@Table(name = "votes")
public class Vote {

@Id
@GeneratedValue
@Column(name = "vote_id")
private Integer voteId;

@ManyToOne
@JoinColumn(name = "uid")
private User user;

@Column(name = "vote_type", length = 8)
@Enumerated(EnumType.STRING)
private VoteType voteType;

@ManyToOne
@JoinColumn(name = "message_id")
private Message message;

最佳答案

你做错了类型转换。 Hibernate 根据您的请求返回查询中的所有对象。但是您需要使用索引 0 处的 Vote 对象。

List<Object[]> list = sessionFactory.getCurrentSession()
.createQuery("from Vote as v left join v.message as m " +
"where m.question=:question and v.user=:user and v.voteType=:voteType")
.setParameter("question", question)
.setParameter("user", user)
.setParameter("voteType", VoteType.MESSAGE)
.list();
System.out.println(list.get(0)[0].getMessage().getNumber());

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

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