package com.vladmihalcea.book.hpjp.hibernate.mapping.enums;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;

/**
 * @author Vlad Mihalcea
 */
public class PostgreSQLEnumType extends org.hibernate.type.EnumType {

    public void nullSafeSet(
            PreparedStatement st,
            Object value,
            int index,
            SharedSessionContractImplementor session)
            throws HibernateException, SQLException {
        if (value == null) {
            st.setNull(index, Types.OTHER);
        } else {
            st.setObject(index, value.toString(), Types.OTHER);
        }
    }
}

@@ -587,139 +587,142 @@
     
-if (value == null) %7B%0A            st.setNull(index, Types.OTHER);%0A        %7D else %7B%0A            st.setObject(index, value.toString(),
+st.setObject(%0A            index,%0A            value != null ?%0A                ((Enum) value).name() :%0A                null,%0A           
  Typ
@@ -733,10 +733,8 @@
 THER
-);
 %0A   
@@ -738,17 +738,18 @@
         
-%7D
+);
 %0A    %7D%0A%7D

USe the Enum name, not the toString method when binding the parameter value