1

I am using Spring Boot.

I have the following table:

CREATE TYPE photo_status as enum('pending', 'completed', 'processing', 'failed');
CREATE TABLE photos (
    uuid uuid DEFAULT gen_random_uuid() PRIMARY KEY,
    url text NOT NULL,
    status photo_status DEFAULT 'pending' NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);

When trying to save to the database, I get the following error:

ERROR: column "status" is of type photo_status but expression is of type character varying

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement

I think my problem is with the emum (PhotoStatus) in the Entity.

Question

I am unable to modify the SQL script to ALTER the table. So how can I change my Entity to work with the existing table?

Code

Entity

@Entity
@Table(name = "photos")
public class PhotoEntity {

    public enum PhotoStatus {
        pending,
        complete,
        processing,
        failed
    }

    @Id
    @Column(name = "uuid")
    private UUID uuid;

    @Column(name = "url", nullable = false)
    private String url;

    @Enumerated(EnumType.STRING)
    @Column(columnDefinition = "photo_status", nullable = false)
    private PhotoStatus status;

    @Column(name = "created_at", nullable = false)
    private LocalDateTime created_at;
Richard
  • 8,193
  • 28
  • 107
  • 228
  • Is this table auto-generated (provided above)? – priyank-sriv Apr 28 '19 at 15:55
  • Check out [this](https://vladmihalcea.com/the-best-way-to-map-an-enum-type-with-jpa-and-hibernate/) article. In particular take a look at `Mapping a Java Enum to a database-specific Enumarated column type` part – Denis Zavedeev Apr 28 '19 at 15:56
  • @Sindbad90, thanks for the reply. Yes the SQL script that creates the table cannot be changed. I forgot to add the creation of the TYPE (CREATE TYPE photo_status as enum('pending', 'completed', 'processing', 'failed');). - added now. – Richard Apr 28 '19 at 15:57
  • 1
    Or [this](https://stackoverflow.com/questions/27804069/hibernate-mapping-between-postgresql-enum-and-java-enum) question – Denis Zavedeev Apr 28 '19 at 15:58

0 Answers0