java - spring-data-jpa 存储库在查询中使用 Collection 作为 @Param

标签 java hibernate jpa spring-data spring-data-jpa

我在通过使用 spring 数据 jpa 存储库执行自定义查询时遇到问题。

@Query annitated 方法不指定 List<> 类型的参数。

这是实体类:

@Entity
@Table(name = "\"user\"")
public class User {
  @Id
  @GeneratedValue(generator = "increment")
  private long id;

  @Column(unique = true, nullable = false)
  private String mail;

  @ManyToMany(targetEntity = Interest.class, mappedBy = "users")
  private List<Interest> interests = new ArrayList<Interest>();
  ...
  ... setters and getters.

@Entity
@Table(name = "interest")
public class Interest {
  @Id
  @Column(name = "interest_name", nullable = false, unique = true)
  private String name;

  @ManyToMany(targetEntity = User.class, fetch = FetchType.LAZY)
  private List<User> users = new ArrayList<User>();
  ...
  ... setters and getters.

这是查询:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
  @Query("select distinct u from User u where u <> :currentUser and
         u.interests in :currentInterests")
  List<User> getUsersWithSameInterests(@Param("currentUser") User user,
         @Param("currentInterests") List<Interest> interests);
}

我用过这个:
@Autowired
private UserRepository userRepository;
  @Override
  public List<User> getUsersWithSameInterests(User user) {
    return userRepository.getUsersWithSameInterests(user, user.getInterests());
  }

但得到
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
.
.
.
Caused by: java.sql.SQLException: No value specified for parameter 2
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2205)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2185)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2115)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1936)
at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:83)
at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:83)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
... 73 more

没有为参数 2 指定值,尽管第二个参数具有有效值

最佳答案

   Change the code as per below:

   Old Code:

   `@Repository
   public interface UserRepository extends JpaRepository<User, Long> {
   @Query("select distinct u from User u where u <> :currentUser and
         u.interests in :currentInterests")
   List<User> getUsersWithSameInterests(@Param("currentUser") User user,
         @Param("currentInterests") List<Interest> interests);
   }`

   Updated Code:

   `@Repository
   public interface UserRepository extends JpaRepository<User, Long> {
   @Query("select distinct u from User u where u.user :currentUser 
           and u.interests in :currentInterests")
   List<User> getUsersWithSameInterests(@Param("currentUser") User user,
         @Param("currentInterests") List<Interest> interests);
  }`    

关于java - spring-data-jpa 存储库在查询中使用 Collection 作为 @Param,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36605532/

相关文章:

java - ^^ 上的字符串模式 [Java]

hibernate - 大部分是不可变的 NaturalId

java - org.hibernate.MappingException : Composite primary key as a foreign key in another table

sql - JPA with Hibernate - 多对多关系,获取所有数据

java - Micronaut 嵌入式服务器与本地主机

Java 一次性模式

java - Oracle xdb-xmlparser 源代码

java - Primefaces 更新破坏变音符号

java - Spring Boot jpa 中多对一映射中的子表中的外键未更新

java - 如何将不同方案的两个事务合并为一个?