简体   繁体   English

Spring 引导 Postgres JPA,不返回主键以响应 findAll()

[英]Spring Boot Postgres JPA, not returning the Primary key in response of findAll()

Employee Class员工 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 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.database-platform = org.hibernate.dialect.PostgreSQLDialect

spring.jpa.show-sql = true spring.jpa.show-sql = true

spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.format_sql=true

spring.jpa.hibernate.ddl-auto = update spring.jpa.hibernate.ddl-auto = 更新

When I send a get request to /api/v1/employees the respone I get is当我向/api/v1/employees发送获取请求时,我得到的响应是

[ { "firstName": "aa", "lastName": "bb", "emailId": "tom@gmail.com" } ] [ {“名字”:“aa”,“姓氏”:“bb”,“emailId”:“tom@gmail.com”}]

And I expect is [ { "eid": 1, "firstName": "aa", "lastName": "bb", "emailId": "tom@gmail.com" } ]我希望是[ { "eid": 1, "firstName": "aa", "lastName": "bb", "emailId": "tom@gmail.com" } ]

The query being run by hibernate includes the eid in select query, still I am not getting it in response hibernate 正在运行的查询包括 select 查询中的 eid,但我仍然没有得到响应hibernate 运行的查询包括选择查询中的 eid,但我仍然没有得到它的响应

I believe you need a getter for eid on the Employee class.我相信您需要一个 getter 用于 Employee class 的 eid。

Add the method to your Employee class.将该方法添加到您的 Employee class。

public long getEid() {
   return this.eid;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM