新记录未获得生成的 ID

New record does not get a generated id

我创建了一个新的 table,它在启动时通过 FlywayDB 迁移:

CREATE TABLE IF NOT EXISTS place_address (
  id bigint,
  place_id bigint NOT NULL,
  street character varying(255) NOT NULL,
  postal_code character varying(255) NOT NULL,
  city character varying(255) NOT NULL,
  country character varying(255) NOT NULL,
  address_type character varying(255) NOT NULL,

  FOREIGN KEY (place_id) REFERENCES place(id),

  CONSTRAINT place_address_pkey PRIMARY KEY (id)
);

我正在创建这样一个实体并将其存储在 JpaRepository:

PlaceAddressEntity placeAddressEntity = new PlaceAddressEntity();
placeAddressEntity.setPlace(placeEntity);
placeAddressEntity.setStreet(establishmentDto.getAddress().getStreet());
placeAddressEntity.setCity(establishmentDto.getAddress().getCity());
placeAddressEntity.setCountry(establishmentDto.getAddress().getCountry());
placeAddressEntity.setPostalCode(establishmentDto.getAddress().getPostalCode());
placeAddressEntity.setAddressType(AddressType.GEOGRAPHIC);

this.placeAddessRepository.save(placeAddressEntity);

然而,这就是我得到的:

2018-09-21 18:42:05.319  WARN 13842 --- [io-8443-exec-10] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 23502
2018-09-21 18:42:05.319 ERROR 13842 --- [io-8443-exec-10] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: null value in column "id" violates not-null constraint
  Detail: Failing row contains (null, 8, Western Road 2-20, NW10 7LW, Greater London, United Kingdom, GEOGRAPHIC).
2018-09-21 18:42:05.322  INFO 13842 --- [io-8443-exec-10] m.a.s.e.CustomRestExceptionHandler       : org.springframework.dao.DataIntegrityViolationException
2018-09-21 18:42:05.323  INFO 13842 --- [io-8443-exec-10] m.a.s.e.CustomRestExceptionHandler       : org.hibernate.exception.ConstraintViolationException

实体定义为:

@Entity
@Table(name = "place_address")
public class PlaceAddressEntity {

    public enum AddressType {
        GEOGRAPHIC,
        BILLING
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "place_id", nullable = false)
    private PlaceEntity place;

    @Column
    private String street;

    @Column(name = "postal_code")
    private String postalCode;

    @Column
    private String city;

    @Column
    private String country;

    @Column(name = "address_type")
    @Enumerated(EnumType.STRING)
    private AddressType addressType;

}

我不知道问题出在哪里 - 知道我错过了什么吗?


这里还有PlaceAddressRepository:

public interface PlaceAddessRepository extends JpaRepository<PlaceAddressEntity, Long> {

}

这是因为您没有使用自增功能

使用id SERIAL PRIMARY KEY,必须在table定义中设置。

当我看到@Antoniosss 的回答时,我正要 post 回答。但是,我 post 我只是为了让我们看看 SERIAL,或者在我的情况下 BIGSERIAL 为我们做了什么。

我能够让它与这个一起工作:

CREATE SEQUENCE IF NOT EXISTS public.place_address_id_seq
  START WITH 1
  INCREMENT BY 1
  NO MINVALUE
  NO MAXVALUE
  CACHE 1;

ALTER SEQUENCE public.place_address_id_seq OWNED BY public.place_address.id;

ALTER TABLE ONLY public.place_address ALTER COLUMN id SET DEFAULT nextval('public.place_address_id_seq'::regclass);

不过,我更喜欢这一款:

id BIGSERIAL PRIMARY KEY,