spring data pg存储json字符串

maven

        
            org.postgresql
            postgresql
            9.3-1102-jdbc4
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

domain

@Entity
public class Demo {

    @javax.persistence.Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String title;
    
    
    @NotNull
    @Column(columnDefinition="text")
    @Convert(converter = ItemConverter.class)
    private List data;

   
    //......
}    

converter

@Converter
public class ItemConverter implements AttributeConverter, String> {
    @Override
    public String convertToDatabaseColumn(List items) {
        return JSON.toJSONString(items);
    }

    @Override
    public List convertToEntityAttribute(String s) {
        return JSON.parseObject(s,new TypeReference>(){});
    }
}

doc

  • JPA and PostqreSQL: long string persistence

你可能感兴趣的:(json,java)