Pregunta

I am not very familiar with jpql and my last experience with sql is some years ago. But let's try to explain what i want. In my current app (Jpa/Eclipselink) i got some easy queries running but now i need some more sophisticated queries.

First the current query that yields "The expression is not a valid conditional expression":

"Select count(a) From UserState u "
+ "Where u.state =  UserVotingState.DONE "
+ "And a.choice.choiceID = :p "
+ "Join AnswerEntity a.choice.question.voting.votingID  u.voting.votingID"

You should get some idea of the structure but here are details. It is an App to organize all kinds of voting or polls. Currently i am writing queries to compute results of voting (count answers). Here is a minimized er-diagram:

voting 1<->n question 1<->n choice 1<->n answer(one per userId)

A user can add answers (for multiple/ single choice). And set a state if user want to finish voting ("DONE"):

voting 1<->n userState (has userId, votingId and State)

My query should count all answers for given choiceId but only for users that have set state DONE for the voting. So i have to join 2 tables but their connection is rather indirect. Could some one plaese give a JPQL query for that?

¿Fue útil?

Solución

try this one out with a subselect:

em.createQuery("SELECT count(a) FROM AnswerEntity a WHERE a.choice.choiceID = :p "
                    + "AND a.userId IN "
                    + "(SELECT us.userId FROM UserState us WHERE us.state = com.company.model.UserVotingState.DONE)");

Also keep in mind to set fully qualified class name for your Enum.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top