对于Spring的@GeneratedValue注释,我有一个问题。
这是我的课:
@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;
}
}
当我尝试添加一个新产品时,我会得到以下错误:
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).
下面是我的服务类中的save方法:
@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)));
}
老实说我不知道这是怎么发生的。我在某个地方读到GenerationType.IDENTITY
不受PostgreSQL支持,但我不认为是这样的,因为我对Category
和Manufacturer
表使用了相同的生成类型,并且我可以为它们添加新的元组。我尝试使用generation和--它可以工作,但是我不想使用那个生成类型,我不明白为什么IDENTITY 在这里不能工作--当它在其他when表上工作时,。有什么想法吗?
编辑1:用GenerationType.AUTO
测试了它,但是仍然显示了类型标识的相同错误。
编辑2:共享用于创建产品表和类别表的SQL。
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;
最终编辑:我的问题已经回答了,由于某些原因,我的主键没有被设置为自动增量,这导致了问题。我相信这是因为我只使用@Id
注释创建了表,创建之后我添加了生成策略(它没有将id
设置为自动增量)。
发布于 2021-12-06 19:10:36
要使它起作用,您必须更改产品表中的某些内容。
@GeneratedValue(Strategy= GenerationType.IDENTITY)只要ID列是串行类型就可以工作。
检查ID列是否为该类型。示例:
CREATE TABLE Product(
id SERIAL NOT NULL,
name VARCHAR(20),
etc...
)
https://stackoverflow.com/questions/70249351
复制相似问题