How to set an @Id primary key that is NOT Auto-Generated in Java JPA/Hibernate?

Ada*_*dam 5 java postgresql hibernate jpa spring-boot

I am using Postgres with Java JPA/Hibernate and want to have the id field as one that is MANUALLY GENERATED by me. i.e. whenever i create an instance of this object, i set the id field anyway.

I've tried for weeks but keep running into either: "Required identifier property not found for class" or "After saving, the identifier must not be null".

Here is a sample of the model class i am using:

import javax.persistence.*;

@Entity
@Table(name = "pojo")
public class Pojo {

    @Id
    @Column(name = "id_one")
    private int idOne;

    @Column(name = "bool_example")
    private boolean boolExample;

    public Pojo(){};

    public Pojo(int idOne, boolean boolExample){
        this.idOne = idOne;
        this.boolExample = boolExample;
    }

    public int getIdOne() {
        return idOne;
    }

    public void setIdOne(int idOne) {
        this.idOne = idOne;
    }

    public boolean isBoolExample() {
        return boolExample;
    }

    public void setBoolExample(boolean boolExample) {
        this.boolExample = boolExample;
    }
}
Run Code Online (Sandbox Code Playgroud)

Here is a sample request i'm calling in

    @GetMapping(value = "/plswork")
    public String pojotestone(){
        Pojo newpojo = new Pojo(1, false);
        pojoService.saveThis(newpojo);

        pojoService.test();
        return "yes";
    }
Run Code Online (Sandbox Code Playgroud)

The pojoService calls on pojoRepository.save(T entity). This pojoRepository is from extending CrudRepository so it creates queries on the fly

Ada*_*dam 5

对于所有想知道的人来说,

是的 - 可以有一个手动 id,这在实体具有固有 id 属性的多个用例中是有意义的。(例如信用卡、银行账户等)

至于这个问题,原来是与Spring JDBC不兼容。解决方案是改用 spring-boot-starter-data-jpa,并让存储库扩展 JPARepository 而不是 CrudRepository。