Note that there are some explanatory texts on larger screens.

plurals
  1. POMapping Java enum on Postgres enum with EclipseLink
    text
    copied!<p>I am making first attempts with JPA (EclipseLink implementation) and feel quite stuck:</p> <p>In PostgreSQL I have the following db schema</p> <pre><code>CREATE TYPE mood AS ENUM ( 'sad', 'happy', 'enthusiastic' ); CREATE TABLE person ( pk BIGINT PRIMARY KEY, name VARCHAR NOT NULL, mood mood NOT NULL ); CREATE SEQUENCE person_pk_seq INCREMENT BY 100 MINVALUE 100; </code></pre> <p>Which works pretty fine, as this insert shows <code>INSERT INTO PERSON (PK, mood, name) VALUES (3, 'happy', 'Joe')</code> (Committing the pk as String makes no difference.)</p> <p>On the JPA side I wrote the following class:</p> <pre><code>package testdb; import java.io.Serializable; import javax.persistence.*; import org.eclipse.persistence.annotations.*; @Entity public class Person implements Serializable { private static final long serialVersionUID = 1L; public enum Mood { sad, happy, enthusiastic; } @Id @SequenceGenerator( name="PERSON_PK_GENERATOR", sequenceName="PERSON_PK_SEQ", allocationSize = 100 ) @GeneratedValue( strategy=GenerationType.SEQUENCE, generator="PERSON_PK_GENERATOR" ) public Long pk; @Enumerated( EnumType.STRING ) @Column( name = "mood" ) @ObjectTypeConverter( name = "moodConverter", objectType = Mood.class, dataType = String.class, conversionValues = { @ConversionValue( objectValue = "sad", dataValue = "sad" ), @ConversionValue( objectValue = "happy", dataValue = "happy" ), @ConversionValue( objectValue = "enthusiastic", dataValue = "enthusiastic" ) }) @Convert( "moodConverter" ) public Mood mood; @Column( name = "name" ) public String name; public static void main(String[] args) { EntityManagerFactory factory = Persistence.createEntityManagerFactory("TestDb.jpa.tests" ); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); Person p = new Person(); em.persist( p ); System.out.println(p.pk); p.name = "Joe"; p.mood = Mood.enthusiastic; em.getTransaction().commit(); Query q = em.createQuery( "select p from Person p" ); Person x = (Person)q.getResultList().get(0); System.out.println( x.pk + " :: " +x.mood ); em.close(); } } </code></pre> <p>However, this example is not working and I have no clue what the problem is:</p> <pre><code>[EL Warning]: 2012-06-05 15:28:20.646--UnitOfWork(845463623)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.BatchUpdateException: Batch-Eintrag 0 INSERT INTO PERSON (PK, mood, name) VALUES ('801', 'enthusiastic', 'Joe') wurde abgebrochen. Rufen Sie 'getNextException' auf, um die Ursache zu erfahren. Error Code: 0 Call: INSERT INTO PERSON (PK, mood, name) VALUES (?, ?, ?) bind =&gt; [3 parameters bound] </code></pre> <p>When I alter the column type of table <code>person</code> to <code>varchar</code> and remove the annotations <code>@Convert</code> and <code>@ObjectTypeConverter</code> everything is working as expected, as well.</p> <p>Any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload