Spring JPA data "null in column xxx violates non-null constraint" in sequential column with postgresql

My entity has a mapOrder field that I want to auto-increment like below:

@Entity
public class Map{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(columnDefinition = "serial")
    private Long mapOrder;

    //.......
}

      

The sql generated seems to be good:

CREATE TABLE map
(
  id bigserial NOT NULL,
  map_order serial NOT NULL,
  ...
)

      

But when I save it with Spring Data JPA datastore like:

Map m=new Map();
repo.save(m);

      

will give me an exception:

Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "map_order" violates not-null constraint

      

Any ideas?

+3


source to share


1 answer


Try changing your code to this:

@GeneratedValue(strategy = GenerationType.SEQUENCE)

      



Link: fooobar.com/questions/2232237 / ...

+1


source







All Articles