2

I have an entity

@Entity
@Table(name = "product")
public class Product {

    @Id
    private Long id;

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

    @Column(name = "condition")
    @Enumerated(value = EnumType.STRING)
    private Condition condition;

    public enum Condition {
        NEW, USED
    }
    // default constructor and getters/setters omitted
}

And Java code which persists an entity

Product product = new Product();
product.setItemName("Gloves");
product.setCondition(Product.Condition.NEW);

entityManager.getTransaction().begin();
entityManager.persist(product);
entityManager.getTransaction().commit();
entityManager.close();

Here is my PostgreSQL db.

Enum type.

CREATE TYPE product_condition AS ENUM ('NEW', 'USED');

And table.

CREATE TABLE product (
id     SERIAL            NOT NULL,
name   VARCHAR(255)      NOT NULL,
condition product_condition NOT NULL,
CONSTRAINT product_pk PRIMARY KEY (id)
);

But cannot insert a new Product into the DB because of

Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: ERROR: column "condition" is of type product_condition but expression is of type character varying
  Hint: You will need to rewrite or cast the expression.
  Position: 55 {prepstmnt 1419086773 INSERT INTO product (id, condition, name) VALUES (?, ?, ?)} [code=0, state=42804]

How can I fix it?

barbara
  • 3,111
  • 6
  • 30
  • 58
  • Do ENUM definition enum Condition { NEW("NEW"), USED("USED") } help, crazy idea but give it a go. Who knows how OpenJPA translates custom sql enum column types. Last resort is ofc using varchar sql column but I guess you want to avoid it. – Whome Jan 01 '15 at 17:58
  • Or this answer http://stackoverflow.com/questions/8709751/can-i-configure-play-to-use-mysql-enums-instead-of-ints/8710305#8710305 works for you? – Whome Jan 01 '15 at 18:00
  • Unfortunately it doesn't work for me. – barbara Jan 01 '15 at 18:30
  • Well since you've told it to persist as a String then it is expecting to do just that, yet your DB has a column of some RDBMS-specific enum type ... and perhaps OpenJPA doesn't support that, what does its docs say? – Neil Stockton Jan 01 '15 at 18:34
  • 1
    I didn't find any mention about that. So yes maybe it doesn't support stuff like that. – barbara Jan 01 '15 at 18:42

0 Answers0