Home > Software design >  JpaRepository query parameters are not getting updated dynamically
JpaRepository query parameters are not getting updated dynamically

Time:12-05

Query parameters are not getting updated -

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.erecruitment.app.model.user;
@Repository
public interface updatePassword extends JpaRepository<user, Long>{
        
    @Transactional
    @Modifying
    @Query(value="UPDATE user SET password=?1 WHERE username=?2",nativeQuery=true)
    int updtPassword(String password,String username);  
}

Result upon execution is -

Hibernate: 
    UPDATE
        user 
    SET
        password=? 
    WHERE
        username=?

I tried hardcoding the parameters like -

@Query(value="UPDATE user SET password='ee' WHERE username='[email protected]'",nativeQuery=true)

And , it worked .But , the first one not worked .Can you please take a minute in helping me , because I am not getting where am I getting wrong?

CodePudding user response:

The above code looks good and worked on my local machine.

Can you please add more details about your question?

CodePudding user response:

Please try:

@Transactional
@Modifying
@Query(value="UPDATE user SET password= :password WHERE username= :username ",nativeQuery=true)
int updtPassword(@Param("password") String password,@Param("username") String username);  

Param class to be imported:

import org.springframework.data.repository.query.Param;
  • Related