我开始进行 Web 开发,但在使用 jpa 时遇到问题。我的 postegre 数据库在手动将数据输入 pgAdmin 时会自动生成一个 id。但是,例如,当尝试通过curl发布帖子时,会出现错误,提示id不能为空,也就是说,id不再自动生成,或者如果是,则在此之前进行验证。我这么说是因为显然其他数据对于注册来说是正确的,并且我知道我的数据库正在工作,因为当我请求获取数据时,数据会出现在客户端上。我正在使用 SpringBoot 和 maven
package com.example.umbrellacorporation.model;
import javax.persistence.*;
@Entity
@Table(name="BOWS")
public class Bows {
@Column(name="NAME")
private String name;
@Column(name="AUTHOR")
private String author;
@Column(name="BIO_AGENT")
private String bio_agent;
@Column(name="ORGANIZATION")
private String organization;
@Column(name = "FOUND")
private String found;
@Column(name = "TYPE")
private String type;
@Column(name = "INFOR")
private String infor;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id" ,columnDefinition = "serial")
private Integer id;
public Bows() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getBio_agent() {
return bio_agent;
}
public void setBio_agent(String bio_agent) {
this.bio_agent = bio_agent;
}
public String getOrganization() {
return organization;
}
public void setOrganization(String organization) {
this.organization = organization;
}
public String getFound() {
return found;
}
public void setFound(String found) {
this.found = found;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getInfor() {
return infor;
}
public void setInfor(String infor) {
this.infor = infor;
}
}
package com.example.umbrellacorporation.controller;
import org.springframework.web.bind.annotation.RestController;
import com.example.umbrellacorporation.model.Bows;
import com.example.umbrellacorporation.model.services.BOWSRepositorys;
import java.lang.Iterable;
import java.util.Optional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@RestController
public class BowsController {
private final BOWSRepositorys bowsRepositorys;
public BowsController(final BOWSRepositorys bowsRepositorys){
this.bowsRepositorys = bowsRepositorys;
}
public Iterable<Bows> getAllBows(){
return this.bowsRepositorys.findAll();
}
@GetMapping("/BOWS/{id}")
public Optional<Bows> getBowsById(@PathVariable Integer id){
return this.bowsRepositorys.findById(id);
}
@PostMapping("/BOWS")
public Bows createNewBows(@RequestBody Bows bows) {
return this.bowsRepositorys.save(bows);
} }
我已经多次更改数据库和 id 属性。这让我相信问题出在代码中。但我没有信心。我希望 jpa 能够识别数据库自动生成的 id,而不是出于某种原因识别 null。
代替这一代的策略
@GeneratedValue(strategy = GenerationType.AUTO)
尝试使用这两个中的任何一个:
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id")
private Integer id;
...或者如果表是基于实体创建的...
@Id
@SequenceGenerator(name = "bows_id_sequence", sequenceName = "bows_id_sequence")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "bows_id_sequence")
@Column(name = "id")
private Integer id;
此外,您的 JpaRepository 必须正确,例如:
public interface BowsRepository extends JpaRepository<Bows, Integer> {
// your customs queries
}