【发布时间】:2019-07-25 06:41:58
【问题描述】:
我有 json 文件 带有 json 对象 作为里面的属性值:
{
"name": "name",
"json": {...}
}
我需要在 RestController 中自动 获取它,并将其用作 JPA+Hibernate 中的实体。
我的实体是:
UPDATE -> 更多指定实体
@Entity
@Table(name = "collections")
public class Collection {
@Id
private String name;
@Column(name = "cache_limit")
private int limit;
@Column(name = "cache_algorithm")
private String algorithm;
@Transient
private JsonNode schema;
@JsonIgnore
@Column(name ="json_schema")
private String jsonSchema;
public Collection() {
}
public String getJsonSchema() {
return schema.toString();
}
public void setJsonSchema(String jsonSchema) {
ObjectMapper mapper = new ObjectMapper();
try {
schema = mapper.readTree(jsonSchema);
} catch (IOException e) {
throw new RuntimeException("Parsing error -> String to JsonNode");
}
}
..setters and getters for name limit algorithm schema..
}
当我使用 entityManager.persist(Collection) 时,我将 json_schema 列为 NULL
我该如何解决?问题可能出在setJsonSchema()中
更新:
public String getJsonSchema() {
return jsonSchema;
}
public void setJsonSchema(JsonNode schema) {
this.jsonSchema = schema.toString();
}
这样的 getter/setter 并不能解决问题
【问题讨论】:
-
“json 字段将为空”是什么意思?你的控制器看起来像
Cat getCat(int name){ return cats.getCatByName(name);},它返回JSON对象,name和json属性为空?
标签: java json spring jpa jackson