I am using Hibernate 5.3.11
I'm trying to link an Enum between my PostgresSQL database and my code.
I referred to these links to make my code :
Hibernate mapping between PostgreSQL enum and Java enum
Java Enums, JPA and Postgres enums - How do I make them work together?
Mapping a Java Enum to a database-specific Enumerated column type
Problem
But I still have that error :
org.postgresql.util.PSQLException: ERROR: the column "weather" is of type weatherenum but the expression is of type integer. Hint: You will need to rewrite or cast the expression.
How solve this problem ?
My code
First, I pass through this method, a meteoDTO that I map into a meteo entity :
public MeteoDTO saveMeteo(MeteoDTO meteoDTO) {
Meteo meteo = MeteoMapper.INSTANCE.meteoFromMeteoDTO(meteoDTO);
this.meteoRepository.save(meteo); // here, there's a error
return MeteoMapper.INSTANCE.meteoDTOFromMeteo(meteo);
}
meteoDTO :
Entity meteo :
Meteo entity
//package and imports
@Entity
@TypeDef(
name = "pgsql_enum",
typeClass = PostgreSQLEnumType.class
)
public class Meteo {
private Integer id;
@Enumerated(EnumType.STRING)
@Column(columnDefinition = "weatherenum")
@Type( type = "pgsql_enum" )
private WeatherEnum weather;
...
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
@Basic
@Column(name = "weather")
public WeatherEnum getWeather() { return weather; }
public void setWeather(WeatherEnum weather) {
this.weather = weather;
}
.
.
.
}
PostgreSQLEnumType
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
);
}
}
}
I tried also this code :
public class PostgreSQLEnumType extends org.hibernate.type.EnumType {
public void nullSafeSet(
PreparedStatement st,
Object value,
int index,
SharedSessionContractImplementor session)
throws HibernateException, SQLException {
st.setObject(
index,
value != null ?
((Enum) value).name() :
null,
Types.OTHER
);
}
}
WeatherEnum
public enum WeatherEnum {
sunny, cloudy, stormy, rainy;
}
PgSQL Script to create enum:
CREATE TYPE WeatherEnum AS ENUM ('sunny','rainy','cloudy','stormy');
Meteo DDL:
--
-- PostgreSQL database dump
--
-- Dumped from database version 10.11 (Ubuntu 10.11-1.pgdg18.04+1)
-- Dumped by pg_dump version 12.1 (Ubuntu 12.1-1.pgdg18.04+1)
-- Started on 2020-01-04 20:00:13 CET
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
SET default_tablespace = '';
--
-- TOC entry 209 (class 1259 OID 33303)
-- Name: meteo; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.meteo (
id integer NOT NULL,
temperature integer,
windforce integer,
windspeed integer,
weather public.weatherenum,
minswell integer,
maxswell integer,
date timestamp without time zone,
idcity integer
);
ALTER TABLE public.meteo OWNER TO postgres;
--
-- TOC entry 208 (class 1259 OID 33301)
-- Name: meteo_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
CREATE SEQUENCE public.meteo_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.meteo_id_seq OWNER TO postgres;
--
-- TOC entry 2961 (class 0 OID 0)
-- Dependencies: 208
-- Name: meteo_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--
ALTER SEQUENCE public.meteo_id_seq OWNED BY public.meteo.id;
--
-- TOC entry 2831 (class 2604 OID 33306)
-- Name: meteo id; Type: DEFAULT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.meteo ALTER COLUMN id SET DEFAULT nextval('public.meteo_id_seq'::regclass);
--
-- TOC entry 2833 (class 2606 OID 33308)
-- Name: meteo meteo_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.meteo
ADD CONSTRAINT meteo_pkey PRIMARY KEY (id);
--
-- TOC entry 2834 (class 2606 OID 33309)
-- Name: meteo meteo_idcity_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.meteo
ADD CONSTRAINT meteo_idcity_fkey FOREIGN KEY (idcity) REFERENCES public.city(id);
-- Completed on 2020-01-04 20:00:13 CET
--
-- PostgreSQL database dump complete
--