Reputation: 1523
I have two classes mapped many to many: User and Topic. What I wanna do is to get all topics who has the provided user. Sometimes (that's the most weird part to me) it gives me this error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 1
My classes looks like
@Entity
@Table(name = "user")
public class User {
/* ... */
@JsonIgnore
@LazyCollection(LazyCollectionOption.TRUE)
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "favorite_topics",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "topic_id"))
private Set<Topic> favoriteTopics;
/* ... */
}
@Entity
@Table(name = "topic")
public class Topic {
/* ... */
@JsonIgnore
@LazyCollection(LazyCollectionOption.TRUE)
@ManyToMany(mappedBy = "favoriteTopics", fetch = FetchType.LAZY)
private Set<User> favoritedBy;
/* ... */
}
@Repository("topicRepository")
public interface TopicRepository extends JpaRepository<Topic, Integer> {
/* ... */
@Query("SELECT t FROM Topic t WHERE :user in t.favoritedBy")
Set<Topic> favoritedBy(@Param("user") User user);
/* ... */
}
The returned query is:
select topic0_.*
from topic topic0_
cross join favorite_topics favoritedb1_, user user2_
where topic0_.topic_id = favoritedb1_.topic_id
and favoritedb1_.user_id = user2_.user_id
and (? in (.))
What am I missing here? The complete code is at github
Upvotes: 0
Views: 97
Reputation: 1523
Instead of use IN statement I used JOIN. This is my query now:
SELECT t FROM Topic t JOIN t.favoritedBy u WHERE :user = u
Upvotes: 0
Reputation: 125
This query isn't providing anything over just reading the user's favouriteTopic association? Just use the association.
Upvotes: 1