Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

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

like image 526
Adam Avatar asked Jun 25 '19 02:06

Adam


People also ask

How can we auto generate primary key in Hibernate?

AUTO: Hibernate selects the generation strategy based on the used dialect, IDENTITY: Hibernate relies on an auto-incremented database column to generate the primary key, SEQUENCE: Hibernate requests the primary key value from a database sequence, TABLE: Hibernate uses a database table to simulate a sequence.

Which JPA annotation is referred to auto generate value for primary key?

SequenceGeneratorJPA annotationDefines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation.

Which annotation is used to auto generate primary key?

When defining a CMP entity bean that uses one of the primary key generators, you use the the @AutomaticKeyGeneration annotation to point to the name of the primary key generator table to obtain primary keys. Also, you must define a primary key field of type Integer or Long to set and get the auto-generated primary key.


1 Answers

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.

like image 191
Adam Avatar answered Sep 27 '22 21:09

Adam