Home > front end >  ERROR: null value in column "id" of relation xxx violates not-null constraint - Spring Dat
ERROR: null value in column "id" of relation xxx violates not-null constraint - Spring Dat

Time:12-07

I have a question regarding the @GeneratedValue annotation from Spring JPA.

This is my class:

@Entity
@NoArgsConstructor
@Getter
@Setter
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private Double price;
    private Integer quantity;

    @ManyToOne
    private Category category;

    @ManyToOne
    private Manufacturer manufacturer;



    public Product(String name, Double price, Integer quantity, Category category, Manufacturer manufacturer) {
        this.name = name;
        this.price = price;
        this.quantity = quantity;
        this.category = category;
        this.manufacturer = manufacturer;
    }
}

And when I try to add a new product I get this error:

2021-12-06 18:03:02.013 ERROR 3720 --- [nio-9090-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: null value in column "id" of relation "product" violates not-null constraint
  Detail: Failing row contains (null, Dress, 333, 33, 1, 1).
2021-12-06 18:03:02.028 ERROR 3720 --- [nio-9090-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [id" of relation "product]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

org.postgresql.util.PSQLException: ERROR: null value in column "id" of relation "product" violates not-null constraint
  Detail: Failing row contains (null, Dress, 333, 33, 1, 1).

Here is the save method from my service class:

@Override
    public Optional<Product> save(String name, Double price, Integer quantity, Long categoryId, Long manufacturerId) {
        Category category = this.categoryRepository.findById(categoryId).orElseThrow(() -> new CategoryNotFoundException(categoryId));
        Manufacturer manufacturer = this.manufacturerRepository.findById(manufacturerId).orElseThrow(() -> new ManufacturerNotFoundException(manufacturerId));

        this.productRepository.deleteByName(name);

        return Optional.of(this.productRepository.save(new Product(name, price, quantity, category, manufacturer)));
    }

Honestly I don't realize how this happens. I read somewhere that GenerationType.IDENTITY is not supported by PostgreSQL, but I don't think that's it because I used the same generation type for the Category and Manufacturer tables and I can add new tuples to them no problem. I tried using generation type SEQUENCE and it works, but I don't want to use that generation type and I don't understand why IDENTITY won't work here when it worked on the other who tables. Any ideas?

Edit 1: Tested it with GenerationType.AUTO and it works, but still shows the same error for type IDENTITY.

Edit 2: Sharing the SQL for creating the Product table and Category table.

CREATE TABLE IF NOT EXISTS public.product
(
    id bigint NOT NULL,
    name character varying(255) COLLATE pg_catalog."default",
    price double precision,
    quantity integer,
    category_id bigint,
    manufacturer_id bigint,
    CONSTRAINT product_pkey PRIMARY KEY (id),
    CONSTRAINT fk1mtsbur82frn64de7balymq9s FOREIGN KEY (category_id)
        REFERENCES public.category (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT fkq5vxx1bolpwm1sngn6krtwsjn FOREIGN KEY (manufacturer_id)
        REFERENCES public.manufacturers (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
)

TABLESPACE pg_default;

ALTER TABLE public.product
    OWNER to wp;
CREATE TABLE IF NOT EXISTS public.category
(
    id bigint NOT NULL DEFAULT nextval('category_id_seq'::regclass),
    description character varying(4000) COLLATE pg_catalog."default",
    name character varying(255) COLLATE pg_catalog."default",
    CONSTRAINT category_pkey PRIMARY KEY (id)
)

TABLESPACE pg_default;

ALTER TABLE public.category
    OWNER to wp;

Final edit: My question has been answered, for some reason my primary key was not set to auto increment and that was causing the problems. I believe it was because I created the table with just the @Id annotation and after it was created I added the generation strategy (which did not set the id to auto increment.

CodePudding user response:

For it to work, you have to change somethings in your Product table.

@GeneratedValue(strategy = GenerationType.IDENTITY) just work if your ID column is SERIAL type.

Check if the ID column is that type. Example:

CREATE TABLE Product(
    id SERIAL NOT NULL,
    name VARCHAR(20),
    etc...
)
  • Related