在spring boot中用json列创建mysql表,hibernate架构验证失败

时间:2017-03-18 14:57:08

标签: json hibernate spring-data

我想创建一个包含 Json 列的表。

我有一个表示该表的实体,其中表示json类型的字段使用json转换器:

实体:

    @Entity
public class GroupEntity {

    @NotNull
    private String groupId;

    @Id
    @NotNull
    private String propertiesStr;


    public GroupEntity() {
        // For JPA
    }

    @Convert(converter = GroupPropertiesJsonConverter.class)
    private HostProperties propertiesDto;

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public String getPropertiesStr() {
        return propertiesStr;
    }

    public void setPropertiesStr(String propertiesStr) {
        this.propertiesStr = propertiesStr;
    }

    public HostProperties getPropertiesDto() {
        return propertiesDto;
    }

    public void setPropertiesDto(HostProperties propertiesDto) {
        this.propertiesDto = propertiesDto;
    }
}

GroupPropertiesJsonConverter 是:

public class GroupPropertiesJsonConverter implements
    AttributeConverter<HostProperties, String> {

    private static final Logger log = Logger.getLogger(GroupPropertiesJsonConverter.class);
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public String convertToDatabaseColumn(HostProperties hostProperties) {
        try {
            return objectMapper.writeValueAsString(hostProperties);
        } catch (JsonProcessingException e) {
            log.error("coudln't convert HostProperties Dto to json, wont persist");
            return null;
        }
    }

    @Override
    public HostProperties convertToEntityAttribute(String dbData) {
        try {
            return objectMapper.readValue(dbData, HostProperties.class);
        } catch (IOException e) {
            log.info("couldn't convert json of properties to Dto", e);
        }
        return null;
    }
}

sql脚本

create table group_entity (
    properties_str varchar(255) not null, 
    group_id varchar(255) not null, 
    properties_dto JSON, primary key (properties_str)
);

当弹出启动应用程序时,我得到以下 hibernate架构验证错误

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [properties_dto] in table [group_entity]; found [json (Types#CHAR)], but expecting [varchar(255) (Types#VARCHAR)]
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.validateColumnType(SchemaValidatorImpl.java:105)
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.validateTable(SchemaValidatorImpl.java:92)
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.doValidation(SchemaValidatorImpl.java:50)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:91)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:475)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)

根据我的理解, Hibernate 将我的实体字段解析为varchar,即使我正在使用转换器注释。

properties_dto JSON更改为properties_dto varchar(255)确实解决了问题,但这不是json类型!

我做错了什么?如何正确配置此表?

2 个答案:

答案 0 :(得分:1)

由于您将其简单地转换为String,就Hibernate而言,它只是一个String属性。 Hibernate似乎不支持开箱即用的JSON,因此您必须实现自己的用户类型才能执行此操作。

这是article describing how to create a JSON user type

答案 1 :(得分:1)

如果你想在实体类中创建一个json类型的列,那么只需使用@Column(columnDefination =&#34; json&#34;)