[英]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
. MyEntityExtentionConverter
是javax.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.