Home > Mobile >  How to solve column id not found in springboot
How to solve column id not found in springboot

Time:06-12

I am trying to get registration details from user and added a logic in spring boot that if email is already exist then throw error. So, when I register with new mail springboot is working fine but when I try with existed mail it shows status 500 error from server. In server, the error is column id not found.

@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

@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

Controller

@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;
}

Error

2022-06-11 11:28:39.677 ERROR 8032 --- [nio-9197-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [Select email from session_handling where email= ?]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query] with root cause

java.sql.SQLException: Column 'id' not found.

CodePudding user response:

@Query(value="Select email from session_handling where email= :email",nativeQuery =true) public SessionHandling findByEmailId(@Param("email")String email);

As per the repository method declaration mentioned by you, the following change require to your Query in order to correct the issue. As per your code you want to return SessionHandling complete object to service class.

@Query(value="Select * from session_handling where email= :email",nativeQuery =true)
  • Related