I want to be able to map a enum declared in java to an enum created in postgres. For example having the following:
CREATE TYPE EYE_COLOR AS ENUM ('BROWN', 'BLUE', 'GREEN');
CREATE TABLE PERSON (
ID INT PRIMARY KEY AUTO_INCREMENT,
NAME NVARCHAR2(128) NOT NULL,
EYE EYE_COLOR
);
In java I have something like this:
public enum EyeColor {
BROWN,
BLUE,
GREEN
}
public class Person {
@Id
Long id;
String name;
EyeColor eye;
}
When I try to do save something to the database like this:
personRepository.save(new Person("test", EyeColor.BROWN))
the sql log seems fine:
Executing prepared SQL statement [INSERT INTO person (name, eye) VALUES (?, ?)]
o.s.jdbc.core.StatementCreatorUtils : Setting SQL statement parameter value: column index 1, parameter value [test], value class [java.lang.String], SQL type 12
o.s.jdbc.core.StatementCreatorUtils : Setting SQL statement parameter value: column index 2, parameter value [BROWN], value class [java.lang.String], SQL type 12
but inside the actual database field I get an exception text:
org.h2.jdbc.JdbcSQLException: General error: "java.lang.RuntimeException: type=25" [50000-196]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:168)
at org.h2.message.DbException.convert(DbException.java:295)
at org.h2.message.DbException.toSQLException(DbException.java:268)
at org.h2.message.TraceObject.logAndConvert(TraceObject.java:352)
at org.h2.jdbc.JdbcResultSetMetaData.getColumnClassName(JdbcResultSetMetaData.java:376)
at com.intellij.database.remote.jdbc.impl.RemoteResultSetImpl.getObject(RemoteResultSetImpl.java:1269)
at com.intellij.database.remote.jdbc.impl.RemoteResultSetImpl.getCurrentRow(RemoteResultSetImpl.java:1249)
at com.intellij.database.remote.jdbc.impl.RemoteResultSetImpl.getObjects(RemoteResultSetImpl.java:1229)
at sun.reflect.GeneratedMethodAccessor13.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.RuntimeException: type=25
If I replace the EYE_COLOR type to NVARCHAR2(128) in the postgres column definition everything works fine. An actual "BROWN" value is inserted.
Any ideas how this should be implemented?
LE: Forgot to add my spring database driver:
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:file:path/to/file;DB_CLOSE_DELAY=-1;MODE=PostgreSQL;AUTO_SERVER=TRUE
I also tried using the ENUM definition like they say in the H2 driver specifications directly inside the column definition, but is still didn't work:
CREATE TABLE PERSON (
ID INT PRIMARY KEY AUTO_INCREMENT,
NAME NVARCHAR2(128) NOT NULL,
EYE ENUM ('BROWN', 'BLUE', 'GREEN')
);