我正在尝试获取所有用户对自定义问题中的邮件的投票:
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;无法施展 ru.kapahgaiii.qa.domain.Vote ru.kapahgaiii.qa.repository.ChatDAOImpl.getVotes(ChatDAOImpl.java:114)
我做错了什么?
Vote.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;
答案 0 :(得分:3)
你做错了演员。 Hibernate会根据您的请求返回查询中的所有对象。但是你需要使用Vote
对象,它位于索引0。
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());
答案 1 :(得分:-1)
尝试这样做:
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(((Vote) list.get(0)).getMessage().getNumber())