2

I have one repository file shared the source code for the same and one controller class through which I make a call to that @query function for one the update statement. I want to update one column data into my Oracle database for which I have written JPA update @query and defined the function into one of the repository files hence making a call from one of my controller class getting could not execute the query.

Repository file

package com.sid.demo.repository;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import com.sid.demo.model.hindalco_model;

public interface update_password extends JpaRepository<hindalco_model,Integer> 
{   
    @Query(value="update USER_REGISTRATION set password = :password WHERE user_name = :user_name and email = :email",nativeQuery=true)
    List<String> findbyuser_update(@Param("password") String password,String user_name,String email);   
}

Controller class

@RequestMapping(value="/update_password",method = RequestMethod.POST)
    public String update_password(@RequestParam String password)
    {
        //String rnumber_verified=randomNumber;
        //int random_otp_verified=Integer.parseInt(rnumber_verified);



        //System.out.println("update query string value = "+hindalco_user_login12);
        System.out.println("user_name outside try = "+user_name12);
        System.out.println("email outside try = "+password12);
        try
        {
            System.out.println("user_inside try = "+user_name12);
            System.out.println("email inside try = "+password12);

            update_password_repo.findbyuser_update(password,user_name12,password12);
            return "Entered password successfully updated"; 
        }
        catch(Exception e)
        {
            return"Iam sorry entered password did not match "+e;
        }

    }

Model Class

package com.sid.demo.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name="user_registration")
public class hindalco_model
{
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Id
    @Column(name="user_id")
    private int user_id;

    @Column(name="user_name")
    private String user_name;

    @Column(name="email")
    private String email;

    @Column(name="password")
    private String password;

    public int getUser_id() {
        return user_id;
    }

    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }



    @Override
    public String toString() {
        return "hindalco_model [user_id=" + user_id + ", user_name=" + user_name + ", email=" + email + ", password="
                + password +"]";
    }



}

I just wanted to write JPA @query to update one of my column data and for that, I want to pass function call from controller class to that repository file which contains @query annotation function definition which will automatically get convert into hibernate query through the compiler and hence in oracle database the data will be updated.

3 Answers 3

5
@Transactional
@Modifying(clearAutomatically = true)
@Query(value="update USER_REGISTRATION set password = ?1 WHERE user_name = ?2 and email = ?3",nativeQuery=true)
void findbyuser_update(String password,String user_name,String email);   

The @Modifying annotation is used to enhance the @Query annotation to execute not only SELECT queries but also INSERT, UPDATE, DELETE, and even DDL queries.

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

4 Comments

Hibernate: UPDATE DATABASE_NAME.USERS_TABLE SET PASSWORD = ? WHERE USER_NAME = ? 2019-03-13 11:54:55.183 WARN 26760 --- [nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1002, SQLState: 24000 2019-03-13 11:54:55.183 ERROR 26760 --- [nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper : ORA-01002: fetch out of sequence
@SAMUELBARKLEY Hello, please share your updated query you have written
@Transactional @Modifying(clearAutomatically = true) @Query(value="UPDATE MINES.HMG_ZEPHYR_USERS SET PASSWORD = :password WHERE USER_NAME = :user_name",nativeQuery=true) List<String> findbyuser_update(@Param("password") String password,String user_name);
@SAMUELBARKLEY update query will not return List<String> so change it to void and try again if it does help then you might need to set autocommit to false
4

For modifying queries (update, delete, insert) you need to use the annotation @Modifying.

Also, your query doesn't return anything so your method should probably return void.

And while not necessary it is always a good idea to name a method according to what it actually is doing.

All this results in:

@Modifying
@Query(value="update USER_REGISTRATION set password = :password WHERE user_name = :user_name and email = :email",nativeQuery=true)
void updatePassword(@Param("password") String password,String user_name,String email);   

3 Comments

Hibernate: UPDATE ss.sid SET PASSWORD = ? WHERE USER_NAME = ? 2019-03-13 11:54:55.183 WARN 26760 --- [nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1002, SQLState: 24000 2019-03-13 11:54:55.183 ERROR 26760 --- [nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper : ORA-01002: fetch out of sequence
@Transactional is also required.
@VXp while a transaction might be required @Transactional is not required on the method.
0

Normally the flow is like Controller -> Service -> Dao. So you can create a Service which will call the respective dao/repository. Also, please check this link.

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.