Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java spring数据jpa存储库在查询中将集合用作@Param_Java_Hibernate_Jpa_Spring Data_Spring Data Jpa - Fatal编程技术网

Java spring数据jpa存储库在查询中将集合用作@Param

Java spring数据jpa存储库在查询中将集合用作@Param,java,hibernate,jpa,spring-data,spring-data-jpa,Java,Hibernate,Jpa,Spring Data,Spring Data Jpa,我在使用SpringDataJPA存储库执行自定义查询时遇到了一个问题 @查询annated方法未指定列表类型的参数 以下是实体类: @Entity @Table(name = "\"user\"") public class User { @Id @GeneratedValue(generator = "increment") private long id; @Column(unique = true, nullable = false) private String

我在使用SpringDataJPA存储库执行自定义查询时遇到了一个问题

@查询annated方法未指定列表类型的参数

以下是实体类:

@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.
没有为参数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);
  }`    
旧代码: `@存储库 公共接口用户存储库扩展了JpaRepository{ @查询(“从用户u中选择不同的u,其中u:currentUser和 u、 利益:当前利益) 列出getUsersWithSameInterests(@Param(“currentUser”)用户, @参数(“当前利益”)列表利益); }` 更新代码: `@存储库 公共接口用户存储库扩展了JpaRepository{ @查询(“从用户u中选择不同的u,其中u.User:currentUser 和美国利益:当前利益) 列出getUsersWithSameInterests(@Param(“currentUser”)用户, @参数(“当前利益”)列表利益); }`
将值放入父项之间,
。。。u、 对(:currentInterests)
感兴趣,然后查看它是否有效。如果命名参数为
?1
?2
,则也可以尝试使用位置占位符。括号和位置占位符都没有帮助。我马上就试过了。
:currentInterests
放在括号内。
@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
   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);
  }`