简体   繁体   English

Map 字符串属性到 JSONB

[英]Map string properties to JSONB

I've been trying map my string properties to Postgresql's JSONB using JPA. I did read perfect article by Vlad Mihalcea many times and also seen relative questions and problems with similar stuff.我一直在尝试使用 JPA 将我的字符串属性 map 转换为 Postgresql 的 JSONB。我确实多次阅读了 Vlad Mihalcea 的完美文章,也看到了类似问题的相关问题。 BUT I still have this exception org.postgresql.util.PSQLException: ERROR: column "json_property" is of type jsonb but expression is of type character varying every time when I'm trying insert something into my table.但是我仍然有这个异常org.postgresql.util.PSQLException: ERROR: column "json_property" is of type jsonb but expression is of type character varying every time when I're trying insert something into my table.

And what even worse is - all these advices in similar questions were useful until I changed my entity class and made him inherits super class. And now situation is like this:更糟糕的是 - 在我更改实体 class 并让他继承超级 class 之前,类似问题中的所有这些建议都是有用的。现在情况是这样的:

  1. If @TypeDef and @Type on my child class and it works great如果我的孩子 class 上的@TypeDef@Type效果很好
  2. But I want use abstraction layer and set annotations, which I noticed above, to my base entity class and after that exception says me 'Hello!但我想使用抽象层并将注释设置为我的基本实体 class,我在上面注意到了这一点,在该异常之后对我说“你好! It's me again'又是我'

My hierarchy is pretty simple and here it is:我的层次结构非常简单,这里是:

Base entity基础实体

@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
@MappedSuperclass
public abstract class AbstractServiceEntity implements Serializable {

private Integer id;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

Child entity子实体

@Entity
@Table(schema = "ref", name = "test_json_3")
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class TestJson extends AbstractServiceEntity {

@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private String jsonProperty;

My table我的桌子

create table ref.test_json_3
(
id serial primary key,
json_property jsonb 
)

UPD I've succesfully inserted record with JPA native query, but I had to unwrap my query into hibernate query. UPD我已经成功地插入了带有 JPA 本机查询的记录,但我不得不将我的查询解包到 hibernate 查询中。 Not sure that it's the most convinient way to manage inserting data into DB.不确定这是管理将数据插入数据库的最方便的方法。 The my question is actual, I still need your help) Example with native query below.我的问题是实际的,我仍然需要你的帮助)下面的本机查询示例。

Code snippent with result带有结果的代码片段

@Repository
public class JpaTestRepository {

@PersistenceContext
private EntityManager entityManager;

@Transactional
public void insert(TestJson testJson) {
    entityManager.createNativeQuery("INSERT INTO test_json_3 (json_property) VALUES (?)")
            .unwrap(Query.class)
            .setParameter(1, testJson.getJsonProperty(), JsonBinaryType.INSTANCE)
            .executeUpdate();
}

Finally I found solution for my problem.最后我找到了解决我的问题的方法。 Answer is - just use your @Column(columnDefinition = "jsonb") and @Type(type = "jsonb" via getters but not class properties.答案是 - 只需使用你的@Column(columnDefinition = "jsonb")@Type(type = "jsonb"通过 getter 而不是类属性。

entity definition实体定义

@Entity
@Table(schema = "ref", name = "test_json_3")
@NoArgsConstructor
@AllArgsConstructor
@Setter
public class TestJson extends AbstractServiceEntity {

private String jsonProperty;

@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
public String getJsonProperty() {
    return jsonProperty;
}
  

You can try to add @TypeDefs under class TestJson:你可以尝试在TestJson类下添加@TypeDefs:

@TypeDefs({
        @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
})
public class TestJson extends AbstractServiceEntity {

Alternate solution for mapping String to Jsonb type.将 String 映射到 Jsonb 类型的替代解决方案。 Just add the following annotation on your string.只需在您的字符串上添加以下注释。

@ColumnTransformer(write = "?::jsonb")
private String jsonProperty;

暂无
暂无

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

相关问题 如何通过 map <string,map<string,integer> &gt; 使用 application.properties </string,map<string,integer> - How to pass a map<String,Map<String,Integer>> with application.properties 春天-属性过滤到地图 <String, Class<Object> &gt; - Spring - properties filte to Map<String, Class<Object>> XSD生成MAP <String, Boolean> 属性 - XSD generate a MAP<String, Boolean> properties Android 工作室商店 map<string, integer> 在带有属性的 file.properties 中</string,> - Android studio store map<String, Integer> in file.properties with properties Java 属性 - 加载到 Map <string, abstractmap.simpleentry<string, string> &gt;</string,> - Java properties - Load into Map<String, AbstractMap.SimpleEntry<String, String>> 将 YAML 属性绑定到 Map <String, List<String> &gt; 使用 Spring Boot 键入 - Bind YAML properties to Map<String, List<String>> type with Spring Boot 申报map <string, list<customclass> > 在 spring application.properties</string,> - Declare a map of <String, List<CustomClass>> in spring application.properties 如何使用格式化字符串“Property:Value”制作属性映射 - how to make a map of properties using formatted string "Property:Value" Map <string, set<pathway> &gt; 将字符串转换为 Map 以设置路径属性之一</string,> - Map<String, Set<Pathway>> conversion to Map of string to set of one of Pathways properties 为什么java.util.Properties实现Map <Object,Object>而不是Map <String,String> - Why does java.util.Properties implement Map<Object,Object> and not Map<String,String>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM