Enum-postgresql-javaspring-Boot-Hibernate-Error

Enum-postgresql-javaspring-Boot-Hibernate-Error,java,postgresql,hibernate,jpa,enums,Java,Postgresql,Hibernate,Jpa,Enums,我正在使用Hibernate 5.3.11 我正在尝试在PostgresSQL数据库和代码之间链接一个枚举 我参考了这些链接来制作我的代码: 问题 但我仍然有一个错误: org.postgresql.util.PSQLException:错误:“weather”列的类型为weatherenum,但表达式的类型为integer。 提示:您需要重写或强制转换表达式 如何解决这个问题 我的代码 首先,我通过这个方法,将一个meteoDTO映射到一个meteo实体: public MeteoDT

我正在使用Hibernate 5.3.11

我正在尝试在PostgresSQL数据库和代码之间链接一个枚举

我参考了这些链接来制作我的代码:



问题
但我仍然有一个错误:

org.postgresql.util.PSQLException:错误:“weather”列的类型为weatherenum,但表达式的类型为integer。 提示:您需要重写或强制转换表达式

如何解决这个问题

我的代码

首先,我通过这个方法,将一个meteoDTO映射到一个meteo实体:

public MeteoDTO saveMeteo(MeteoDTO MeteoDTO){
Meteo Meteo=MeteoMapper.INSTANCE.MeteoFromMeteTo(meteoDTO);
this.meteoRepository.save(meteo);//这里有一个错误
返回MeteoMapper.INSTANCE.meteoDTOFromMeteo(meteo);
}
meteoDTO:

实体meteo:

Meteo实体

//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
            );
        }
    }
}
我还尝试了以下代码:

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
        );
    }
}
天气数

public enum WeatherEnum {
    sunny, cloudy, stormy, rainy;
}
用于创建枚举的PgSQL脚本:

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
--


1) 显示如何在DDL中定义此属性。2) 显示出现问题的代码。好的,对不起,我正在编辑我的问题。我不理解“1”,DDL是什么?DDL显示完整堆栈跟踪,而不仅仅是最后一行。