简体   繁体   English

为什么我的转换列被 JpaAttributeTypeInspection 标记并带有错误消息“'Basic' 属性类型不应该是地图”?

[英]Why is my converted column marked by JpaAttributeTypeInspection with the error message “'Basic' attribute type should not be a map”?

I was trying to store some JSON as a string in a column via JPA and Spring and was following a baeldung tutorial .我试图通过 JPA 和 Spring 将一些 JSON 作为字符串存储在列中,并且正在遵循baeldung 教程 My code like this:我的代码是这样的:

    @Column
    @Convert(converter = MyEntityExtentionConverter.class)
    private Map<String, Object> myEntityExtention;

MyEntityExtentionConverter is an implementation of javax.persistence.AttributeConverter<Map<String, Object>, String> that converts the string back and forth using the Jackson ObjectMapper . MyEntityExtentionConverterjavax.persistence.AttributeConverter<Map<String, Object>, String>的实现,它使用 Jackson ObjectMapper来回转换字符串。

According to mentioned tutorial this should have been it, however now I get an Error that根据提到的教程这应该是它,但是现在我得到一个错误

'Basic' attribute type should not be a map “基本”属性类型不应为 map

Theoretically I could disable it by adding @SuppressWarnings("JpaAttributeTypeInspection") to the annotations, but that feels like ignoring rather than solving the error.从理论上讲,我可以通过在注释中添加@SuppressWarnings("JpaAttributeTypeInspection")来禁用它,但这感觉就像忽略而不是解决错误。 What am I doing wrong here?我在这里做错了什么?

You have to annotate prop "myEntityExtention" with @Type but is not possible to add both @Type and @Convert..您必须使用 @Type 注释道具“myEntityExtention”,不能同时添加 @Type 和 @Convert..

as you can see in this tutorial you have to define the json type on the top of your entity:正如您在本教程中看到的,您必须在实体顶部定义 json 类型

@Entity
@Table(name = "some_table_name")
@TypeDef(name = "json", typeClass = JsonStringType.class)
public class CustomEntity {

then add @Type annotation instead @Convert :然后添加@Type注释而不是@Convert

@Type( type = "json" )
private Map<String, Object> myEntityExtention;

be sure to add all the right dependencies/versions.确保添加所有正确的依赖项/版本。

IE i'm using hibernate 5.4 so my dependencies are: IE 我正在使用 hibernate 5.4 所以我的依赖项是:

<!-- Hibernate ORM core version 5.4.21.Final (inherited from spring-boot 2.3.4)-->
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>        
 </parent>
<dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-hibernate5</artifactId>
        <version>2.8.4</version>
    </dependency>
    <dependency>
            <groupId>com.vladmihalcea</groupId>
            <artifactId>hibernate-types-52</artifactId>
            <!--for hibernate >= 5.2-->
            <version>2.10.2</version>
    </dependency>
</dependencies>

Look like this is an issue from IntelliJ IDEA:看起来这是来自 IntelliJ IDEA 的问题:

https://youtrack.jetbrains.com/issue/IDEA-270687 https://youtrack.jetbrains.com/issue/IDEA-270687

We can use workaround by this way: Using the @SuppressWarnings("JpaAttributeTypeInspection") annotation removes the warning.我们可以通过这种方式使用解决方法:使用@SuppressWarnings("JpaAttributeTypeInspection")注释删除警告。

That field is not meant to be persisted.该字段不应该被持久化。 Remove the @Column annotation and use @Transient.删除@Column 注释并使用@Transient。 You are supposed to persist it as a JSON which will be done in the customerAttributeJSON, when reading from the database the customerAttributes will be populated and you can use it with DTOs.您应该将其保留为 JSON,这将在 customerAttributeJSON 中完成,当从数据库中读取 customerAttributes 时,您可以将其与 DTO 一起使用。

 @Entity @Table(name = "Customers") public class Customer { @Id private int id; private String firstName; private String lastName; private String customerAttributeJSON; @Transient @Convert(converter = HashMapConverter.class) private Map<String, Object> customerAttributes; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的关联数组转换为 object PHP? - Why my associative array is converted to object PHP? 错误类型 JSONArray 无法转换为 JSONObject - error type JSONArray cannot be converted to JSONObject 为什么要在我的自定义错误消息中添加“无效的JSON” - Why getting “Invalid JSON” added to my custom error message 为什么GCM下游HTTP消息响应json中的“结果”参数标记为可选? - Why is the “results” parameter in the GCM downstream HTTP message response json marked as optional? 为什么我的多部分表单数据会转换为JSON? - Why does my multipart form-data get converted to JSON? 为什么我的字符串没有在 jQuery 中转换为数组 - Why isn't my string being converted into an Array in jQuery 如何使用 jsonapi 1.0 在属性中输入 map 复杂类型 - how to map complex type in Attribute with jsonapi 1.0 从用户收到的Json无法转换为我的webserver API中的类型 - Json received from user cannot be converted to type in my webserver API 为什么我的JavaScript对象通过返回转换为字符串? - Why does my JavaScript object get converted to a string by returning it? 为什么我的“<br /> ”标签被转换为“&lt;br /&gt;”? - Why are my “<br />” tags getting converted to “&lt;br /&gt;”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM