枚举-PostgresSQL-Java Spring Boot-休眠-错误

时间:2020-01-04 13:51:01

标签: java postgresql hibernate jpa enums

我正在使用Hibernate 5.3.11

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

我引用这些链接来编写代码:

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

问题
但是我仍然有这个错误:

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

如何解决这个问题?

我的代码

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

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:

enter image description here

实体气象:

enter image description here

流星实体

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

0 个答案:

没有答案
相关问题