Home > Net >  Spring Boot Postgres JPA, not returning the Primary key in response of findAll()
Spring Boot Postgres JPA, not returning the Primary key in response of findAll()

Time:01-07

Employee Class

@Entity
@Table(name = "employees")
public class Employee {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "eid", unique = true, nullable = false)
    private long eid;
    
    @Column(name ="first_name")
    private String firstName;
    
    @Column(name ="last_name")
    private String lastName;

    @Column(name ="email_id")
    private String emailId;
        
    
    public Employee() {
        
    }
    
    public Employee(String firstName, String lastName, String emailId) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.emailId = emailId;
    }
    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 getEmailId() {
        return emailId;
    }
    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }   
}

Employee Repo

@Repository
public interface EmployeeRepo extends JpaRepository<Employee, Long>{
}

Controller Class

@CrossOrigin(origins = {"http://localhost:3000/"})
@RestController
@RequestMapping("/api/v1/")
public class EmployeeController {

    @Autowired  //Creates object internally on runtime
    private EmployeeRepo employeeRepo;
    
    //get all employee
    @GetMapping("/employees")
    public List<Employee> getAllEmployees(){
        return employeeRepo.findAll();      
    }
}

Application.properties

spring.jpa.database-platform = org.hibernate.dialect.PostgreSQLDialect

spring.jpa.show-sql = true

spring.jpa.properties.hibernate.format_sql=true

spring.jpa.hibernate.ddl-auto = update

When I send a get request to /api/v1/employees the respone I get is

[ { "firstName": "aa", "lastName": "bb", "emailId": "[email protected]" } ]

And I expect is [ { "eid" : 1, "firstName": "aa", "lastName": "bb", "emailId": "[email protected]" } ]

The query being run by hibernate includes the eid in select query, still I am not getting it in response The query being run by hibernate includes the eid in select query, still I am not getting it in response

CodePudding user response:

I believe you need a getter for eid on the Employee class.

Add the method to your Employee class.

public long getEid() {
   return this.eid;
}
  •  Tags:  
  • Related