我正在嘗試從用戶那里獲取注冊詳細資訊,并在 Spring Boot 中添加了一個邏輯,如果電子郵件已經存在,則拋出錯誤。因此,當我注冊新郵件時,springboot 作業正常,但是當我嘗試使用現有郵件時,它顯示來自服務器的狀態 500 錯誤。在服務器中,column id沒有發現錯誤。
@Repository
@Transactional
public interface SessionHandlingRepository extends JpaRepository<SessionHandling, Integer>{
@Query(value="Select email from session_handling where email= :email",nativeQuery =true)
public SessionHandling findByEmailId(@Param("email")String email);
@Query(value="Select email, password from session_handling where email= :email AND password= :password",nativeQuery =true)
public SessionHandling findByEmailIdAndPassword(@Param("email")String email, @Param("password")String password);
}
物體
@Entity
public class SessionHandling {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String email;
private String password;
private String cpassword;
public SessionHandling(int id, String name, String email, String password, String cpassword) {
super();
this.id = id;
this.name = name;
this.email = email;
this.password = password;
this.cpassword = cpassword;
} //all getter and setter
控制器
@Autowired
private SessionHandlingService service;
@PostMapping("/register")
public SessionHandling addUser(@RequestBody SessionHandling registration) throws Exception {
String tempEmailId = registration.getEmail();
if(tempEmailId != null && !"".equals(tempEmailId)) {
SessionHandling UserObj = service.fetchUserByEmailId(tempEmailId);
if(UserObj != null) {
throw new Exception("user with " tempEmailId "already exist");
}
}
SessionHandling UserObj = null;
UserObj = service.addUser(registration);
return UserObj;
}
錯誤
2022-06-11 11:28:39.677 錯誤 8032 --- [nio-9197-exec-1] oaccC[.[.[/].[dispatcherServlet]:Servlet.service() 用于 servlet [dispatcherServlet] path [] 拋出例外 [請求處理失敗;嵌套例外是 org.springframework.dao.InvalidDataAccessResourceUsageException:無法執行查詢;SQL [Select email from session_handling where email= ?]; 嵌套例外是 org.hibernate.exception.SQLGrammarException: could not execute query] 根本原因
java.sql.SQLException:找不到列“id”。
uj5u.com熱心網友回復:
@Query(value="Select email from session_handling where email= :email",nativeQuery =true) public SessionHandling findByEmailId(@Param("email")String email);
根據您提到的存盤庫方法宣告,您需要進行以下更改Query才能更正問題。根據您的代碼,您希望將SessionHandling完整的物件回傳給服務類。
@Query(value="Select * from session_handling where email= :email",nativeQuery =true)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/490385.html
