-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Insert Hibernate 4 with JPA 2 and Postgresql 9 enum type
PostPosted: Wed Dec 05, 2012 4:47 pm 
Newbie

Joined: Wed Dec 05, 2012 4:28 pm
Posts: 1
Hi,

I'm using Postgresql 9.1 with Eclipse/Java 6, Hibernate 4.1.7, JPA 2

Code:
[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.


I know I can fake all this using a varchar(255) type and read/write the enum "name" or using an "integer" column and use @PostLoad and @PrePersist lifecycle but I was hoping this would just work. Any ideas?

Thx
-Rob


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.