Reputation: 63
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.
Upvotes: 2
Views: 5201
Reputation: 81862
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);
Upvotes: 4
Reputation: 650
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.
Upvotes: 0
Reputation: 13727
@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.
Upvotes: 5