Warm tip: This article is reproduced from serverfault.com, please click

Springboot Hibernate read data from existing table on SQL Server

发布于 2020-12-03 14:42:06

I have an existing Table in SQL Server.

CREATE TABLE [dbo].[STUDENTS](
    [ID] [int] NOT NULL,
    [FIRSTNAME] [varchar](50) NOT NULL,
    [LASTNAME] [varchar](50) NOT NULL,
    [EMAIL] [varchar](100) NOT NULL,
    [AGE] [int] NOT NULL,
    [ADDRESS] [varchar](3000) NOT NULL,
    PRIMARY KEY (ID)
);

I have created this entity in JAVA to me able to extract data using Hibernate.

package com.My_task_be.spring.model;

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

@Entity
@Table(name = "STUDENTS")
public class Student {

    @Id
    @Column(name = "ID")
    public int id;
    @Column(name = "FISRTNAME")
    public String firstName;
    @Column(name = "LASTNAME")
    public String lastName;
    @Column(name = "EMAIL")
    public String email;
    @Column(name = "AGE")
    public int Age;
    @Column(name = "ADDRESS")
    public String Address;
    
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public int getAge() {
        return Age;
    }
    public void setAge(int age) {
        Age = age;
    }
    public String getAddress() {
        return Address;
    }
    public void setAddress(String address) {
        Address = address;
    }
    
    
}

This is the repository

@Repository
public interface StudentRepository extends CrudRepository<Student, Integer>{
    List<Student> findAll();
    
}

This is the controller

@RestController
public class AppController {

    @Autowired
    private StudentRepository studentRpository;
    
    @GetMapping("/getAllStudents")
    public List<Student> getAllStudents(){
        return studentRpository.findAll();
    }
    
    @GetMapping("/helloWorld")
    public String helloWorld(){
        return "Hello World!";
    }
    
}

When i run http://localhost:8080/getAllStudents

I get Invalid column name 'fisrtname'.

I dont want hibernate to create a new table I just want it to read from an existing table. And I have specified the Column names but still it did not work.

Any ideas?

Questioner
jayzee
Viewed
0
Tushar 2020-12-03 22:46:22

You have a typo in FISRTNAME. It is FIRSTNAME, change as below

@Column(name = "FIRSTNAME")
public String firstName;