Spring 启动 Postgres JPA,不返回主键以响应 findAll()
Spring Boot Postgres JPA, not returning the Primary key in response of findAll()
员工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;
}
}
员工回购
@Repository
public interface EmployeeRepo extends JpaRepository<Employee, Long>{
}
控制器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-平台=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql = 真
spring.jpa.properties.hibernate.format_sql=真
spring.jpa.hibernate.ddl-自动=更新
当我向 /api/v1/employees 发送获取请求时,我得到的响应是
[
{
"firstName": "aa",
"lastName": "bb",
"emailId": "tom@gmail.com"
}
]
我预计是
[
{
“开斋节”:1,
"firstName": "aa",
"lastName": "bb",
"emailId": "tom@gmail.com"
}
]
hibernate 正在 运行 的查询包括 select 查询中的 eid,但我仍然没有得到它的响应
我认为您需要 getter 为员工 class 开斋节。
将方法添加到您的员工 class。
public long getEid() {
return this.eid;
}
员工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;
}
}
员工回购
@Repository
public interface EmployeeRepo extends JpaRepository<Employee, Long>{
}
控制器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-平台=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql = 真
spring.jpa.properties.hibernate.format_sql=真
spring.jpa.hibernate.ddl-自动=更新
当我向 /api/v1/employees 发送获取请求时,我得到的响应是
[ { "firstName": "aa", "lastName": "bb", "emailId": "tom@gmail.com" } ]
我预计是 [ { “开斋节”:1, "firstName": "aa", "lastName": "bb", "emailId": "tom@gmail.com" } ]
hibernate 正在 运行 的查询包括 select 查询中的 eid,但我仍然没有得到它的响应
我认为您需要 getter 为员工 class 开斋节。
将方法添加到您的员工 class。
public long getEid() {
return this.eid;
}