2

I'm trying to get all user votes for messages in custom question:

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());

And got exception in the last string:

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

What am I doing wrong?

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;
1
  • Can you check with a debugger if list really contains Vote objects? Commented Jan 17, 2015 at 12:36

2 Answers 2

3

You are doing wrong cast. Hibernate returns you all objects from the query as you requested. But you needed to use Vote object, which is at index 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());
Sign up to request clarification or add additional context in comments.

3 Comments

OP, it would at least be interesting to find out what object is at index 1. Your query clearly asks for just one item per row so my guess is that you have not posted the real query you are executing.
@MarkoTopolnik That would probably be a string containing v.message. The OP should use select v from Vote as v left join ... if he just wants to get Vote objects in the list.
@ErwinBolwidt Yes, that's the only explanation I could come up with and if it is correct, then your suggestion is clearly what OP should have done instead of this workaround. However, aren't the join semantics such that they just result in an eagerly loaded message field? Quote: The associated objects are also not returned directly in the query results. Instead, they may be accessed via the parent object. But this applies to fetch joins, so that may be the key difference. Hibernate's docs are generally quite hazy about these semantics, often it takes trial an error to second-guess them.
-1

Try doing it like this:

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())

;

1 Comment

This is what OP is doing, only implicitly. It will fail with the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.