3

I want to put enum in my entity. But I have an error with validation which JPA wonts smallint set as enum. How I can solve this issue.

"Schema-validation: wrong column type encountered in column [status] in table [order]; found [int2 (Types#SMALLINT)], but expecting [int4 (Types#INTEGER)]"[enter image description here] enter image description here

enter image description here

ImtiazeA
  • 1,272
  • 14
  • 19
YWILLS
  • 128
  • 10
  • Did you generate the table using hibernate DDL or SQL query? Your data type for the column is set to `SMALLINT` but here you will `INT`. – ImtiazeA Oct 11 '20 at 13:59
  • in postgres table column is smallint and I can't change it. – YWILLS Oct 11 '20 at 14:01
  • I want put enum entity but it requires int type – YWILLS Oct 11 '20 at 14:02
  • Is this a requirement to store the enum's ordinal values in a column of type SMALLINT or you don't know how to change the data type for that column? – ImtiazeA Oct 11 '20 at 14:05
  • I know how to change column to int, but I have no permission to do it. – YWILLS Oct 11 '20 at 14:06
  • Ideally, you shouldn't store the ordinal values on DB, as the change in the enum can compromise the data integrity, but you can achieve this using column annotation on the enum field in your entity. like `@Column(columnDefinition = "SMALLINT") private OrderStatus orderStatus;` – ImtiazeA Oct 11 '20 at 14:13
  • I tried that but I have the same error. – YWILLS Oct 11 '20 at 14:20
  • Schema-validation: wrong column type encountered in column [status] in table [`order`]; found [int2 (Types#SMALLINT)], but expecting [smallint (Types#INTEGER)] – YWILLS Oct 11 '20 at 14:20
  • see this question https://stackoverflow.com/questions/2751733/map-enum-in-jpa-with-fixed-values – Anton Straka Oct 11 '20 at 15:32

1 Answers1

11

Add columnDefinition="int2" at OrderStatus in your entity.

    @Column(name = "status", columnDefinition = "int2")
    OrderStatus status;

Tested on spring boot 2.2.10

Shawrup
  • 2,478
  • 2
  • 12
  • 21