pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: rmitchell@bzzagent.com
To: pgsql-bugs@postgresql.org
Subject: BUG #7734: JPA2/Hibernate4 PG enum insert causes exception
Date: Wed, 05 Dec 2012 20:54:44 +0000
Message-ID: <E1TgLzQ-0003hz-Gg@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"; 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd";
    version="2.0" >

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

            <property name="hibernate.hbm2ddl.auto" value="validate"/>
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="false" />
            <property name="prefer_sequence_per_entity" value="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="my_table")
public class MyTable {

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

	@Enumerated(EnumType.STRING)
	@Column(name="gender_type", columnDefinition="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 =
Persistence.createEntityManagerFactory("MyPersistenceUnit");
        EntityManager em = emf.createEntityManager();

		MyTable myTable = 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 “gender_type” is
of type GENDER_ENUM but expression is of type character varying
Hint: You will need to rewrite or cast the expression.







-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (2+ messages)  latest in thread

Message-ID: <E1TgLzQ-0003hz-Gg@wrigleys.postgresql.org>
Permalink:  ../E1TgLzQ-0003hz-Gg@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/E1TgLzQ-0003hz-Gg@wrigleys.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: rmitchell@bzzagent.com
  Subject: Re: BUG #7734: JPA2/Hibernate4 PG enum insert causes exception
  In-Reply-To: <E1TgLzQ-0003hz-Gg@wrigleys.postgresql.org>
  References: <E1TgLzQ-0003hz-Gg@wrigleys.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox