Spring JPA 使用“(? in (.))”生成查询
Spring JPA generating query with "(? in (.))"
我有两个 类 多对多映射:用户和主题。我想做的是获取所有拥有提供用户的主题。 有时(这对我来说是最奇怪的部分)它给我这个错误:
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
我的类长得像
@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);
/* ... */
}
返回的查询是:
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 (.))
我在这里错过了什么? The complete code is at github
除了读取用户最喜欢的主题关联之外,此查询没有提供任何内容?就用协会吧。
我没有使用 IN 语句,而是使用了 JOIN。这是我现在的查询:
SELECT t FROM Topic t JOIN t.favoritedBy u WHERE :user = u
我有两个 类 多对多映射:用户和主题。我想做的是获取所有拥有提供用户的主题。 有时(这对我来说是最奇怪的部分)它给我这个错误:
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
我的类长得像
@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);
/* ... */
}
返回的查询是:
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 (.))
我在这里错过了什么? The complete code is at github
除了读取用户最喜欢的主题关联之外,此查询没有提供任何内容?就用协会吧。
我没有使用 IN 语句,而是使用了 JOIN。这是我现在的查询:
SELECT t FROM Topic t JOIN t.favoritedBy u WHERE :user = u