使用spring数据jpa和postgresql保存枚举

x33g5p2x  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(244)

我的数据库中有一个枚举:

CREATE TYPE ABILITY_TYPES AS ENUM ('ATTACK', 'DEFENCE', 'HEAL');

一张table:

CREATE TABLE test(
    id SERIAL PRIMARY KEY,
    name VARCHAR(32),
    ability_type ABILITY_TYPES
);

以及plpgsql函数:

create or replace function test3(name varchar, ability ability_types) returns boolean as 
$$
begin
insert into test(name, ability_type) values(name, ability);
return true;
end
$$ language plpgsql;

现在,我尝试使用springdatajpa和上面的函数保存一个实体。我的java枚举:

public enum AbilityTypes {
ATTACK("attack"),
DEFENCE("defence"),
HEAL("heal");

private String description;

AbilityTypes(String description) {
    this.description = description;
}

public String getDescription() {
    return description;
}
}

我的实体:

@Entity(name = "test")
public class Test {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column(name = "name")
private String name;

@Enumerated(EnumType.STRING)
@Column(name = "ability_type")
private AbilityTypes abilityType;

...
}

据我所知,jpa会打电话来的 Enum.name() 试着救我的命 abilityType 作为 VARCHAR . 那么,有什么方法可以用枚举类型将java枚举保存在列中呢?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题