简体   繁体   中英

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

I am using Postgres with Java JPA/Hibernate and want to have the id field as one that is MANUALLY GENERATED by me. ie 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;
    }
}

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";
    }

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

For all those wondering,

YES - It is possible to have a manual id, and this makes sense in multiple use cases where entities have inherent id attributes. (such as credit cards, bank accounts, etc)

As for this problem, it turned out to be an incompatibility with Spring JDBC. The solution is to use spring-boot-starter-data-jpa instead, and have repositories extend JPARepository instead of CrudRepository.

There are a couple of ways to do that both in Spring Data JDBC( https://docs.spring.io/spring-data/jdbc/docs/2.2.12/reference/html/#is-new-state-detection ) and Spring Data JPA( https://docs.spring.io/spring-data/jpa/docs/2.7.7/reference/html/#jpa.entity-persistence.saving-entites.strategies ). The most straightforward is implementing Persistable<YourIdType interface:

@Entity
@Table(name = "pojo")
public class Pojo implements Persistable<Integer>{

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

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

    @Transient
    private boolean isNew;

    public setIsNew(boolean isNew) {
       this.isNew = isNew;
    }

    @Override
    public boolean isNew() {
      return isNew; 
    }

    //getters/setters/constructors...

}

now if you want to save a new entity with a id set in Java, you just need to call

pojo.setNew(true);
repository.save(pojo);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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