BUG #7734: JPA2/Hibernate4 PG enum insert causes exception - Mailing list pgsql-bugs

From rmitchell@bzzagent.com
Subject BUG #7734: JPA2/Hibernate4 PG enum insert causes exception
Date
Msg-id E1TgLzQ-0003hz-Gg@wrigleys.postgresql.org
Whole thread Raw
Responses Re: BUG #7734: JPA2/Hibernate4 PG enum insert causes exception
List pgsql-bugs
The following bug has been logged on the website:

Bug reference:      7734
Logged by:          Rob Mitchell
Email address:      rmitchell@bzzagent.com
PostgreSQL version: 9.1.4
Operating system:   MacOS X 10.7
Description:        =


[1] Database script

create type GENDER_ENUM as enum ('male', 'female', 'unknown');
create sequence my_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 START 1;
create table my_table (
  id            integer NOT NULL DEFAULT nextval('my_seq'),
  gender_type   GENDER_ENUM NOT NULL,

  CONSTRAINT my_table_pkey PRIMARY KEY(id)
);


insert into my_table (gender) values ('male');
insert into my_table (gender) values ('female');
insert into my_table (gender) values ('unknown');

[2] Persistence.xml file

<?xml version=3D"1.0" encoding=3D"UTF-8"?>
<persistence xmlns=3D"http://java.sun.com/xml/ns/persistence" =

    xmlns:xsi=3D"http://www.w3.org/2001/XMLSchema-instance" =

    xsi:schemaLocation=3D"http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version=3D"2.0" >

    <persistence-unit name=3D"MyPersistenceUnit"
transaction-type=3D"RESOURCE_LOCAL">
        <description>Persistence unit</description>
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name=3D"hibernate.dialect"
value=3D"org.hibernate.dialect.PostgreSQLDialect"/>
            <property name=3D"javax.persistence.jdbc.driver"
value=3D"org.postgresql.Driver" />
            <property name=3D"javax.persistence.jdbc.url"
value=3D"jdbc:postgresql://localhost:5432/mydb" />
            <property name=3D"javax.persistence.jdbc.user" value=3D"user" />
            <property name=3D"javax.persistence.jdbc.password"
value=3D"password" />

            <property name=3D"hibernate.hbm2ddl.auto" value=3D"validate"/>
            <property name=3D"hibernate.show_sql" value=3D"false" />
            <property name=3D"hibernate.format_sql" value=3D"false" />
            <property name=3D"prefer_sequence_per_entity" value=3D"true" />
        </properties>
    </persistence-unit>
</persistence>

[3] Entity code

package com.mypackage;

import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Transient;

@Entity(name=3D"my_table")
public class MyTable {

    @SequenceGenerator(name=3D"generatorMySeq", sequenceName=3D"my_seq",
allocationSize=3D1)
    @GeneratedValue(strategy =3D GenerationType.SEQUENCE, generator =3D
"generatorMySeq")
    @Id
    @Column(name =3D "id")
    private long id;

    @Enumerated(EnumType.STRING)
    @Column(name=3D"gender_type", columnDefinition=3D"GENDER_ENUM")
    private GenderEnum gender;
    =

    // getters and setters
}

[4] My Java enumerated type

public enum GenderEnum {
   male, female, unknown;
}

[5] JUnit test

public class Test_MyTable {

    @Test
    public void test_1() {
        EntityManagerFactory emf =3D
Persistence.createEntityManagerFactory("MyPersistenceUnit");
        EntityManager em =3D emf.createEntityManager();

        MyTable myTable =3D new MyTable();
        myTable.setGender(GenderEnum.male);

        em.getTransaction().begin();
        em.persist(ac);
        em.getTransaction().commit();

        em.close();
        emf.close();
    }
}

[6] Exception

The exception is:

Caused by: org.postgresql.util.PSQLException: ERROR: column =E2=80=9Cgender=
_type=E2=80=9D is
of type GENDER_ENUM but expression is of type character varying
Hint: You will need to rewrite or cast the expression.

pgsql-bugs by date:

Previous
From: toponraja@gmail.com
Date:
Subject: BUG #7736: error reading c:\Program Files\PostgreSQL\9.1\data\postgresql.conf during installation
Next
From: me@nathanalden.com
Date:
Subject: BUG #7741: Two bugs when backing up databases with default privileges defined on public schema