0

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

User.java

@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;

    /* ... */
}

Topic.java

@Entity
@Table(name = "topic")
public class Topic {

   /* ... */

    @JsonIgnore
    @LazyCollection(LazyCollectionOption.TRUE)
    @ManyToMany(mappedBy = "favoriteTopics", fetch = FetchType.LAZY)
    private Set<User> favoritedBy;

   /* ... */
}

TopicRepository.java

@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

3
  • 1
    and where is your code that invokes the JPQL query? including setting the parameters? Post that snippet here not somewhere else Commented Nov 8, 2017 at 8:44
  • @DN1 updated the repository code Commented Nov 8, 2017 at 9:53
  • Make it sure that list (or array of values) you pass in in (.) clause is not empty/null. Commented Nov 8, 2017 at 10:13

2 Answers 2

1

This query isn't providing anything over just reading the user's favouriteTopic association? Just use the association.

Sign up to request clarification or add additional context in comments.

2 Comments

But sometimes when I access the association I get an exception about session
Mysql cant handle null / empty lists when using in, protected against it e.g. (coalesce(:idParam) is null or entity.id in (:idParam)). But you should probably sort of your session exception as any time you need to lazy load an assoc you wont be able to. There are many answers on how to avoid lazy / proxy type errors depending on your stack (@Transaction) as it looks like you are using Spring Data
0

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

Comments

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.