【发布时间】:2017-12-02 10:33:10
【问题描述】:
有这个类:
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private int key;
private String text;
private Tag[] tags;
private String title;
private boolean valid;
public Activity(int key, String text, Tag[] tags, String title) {
this.key = key;
this.text = text;
this.tags = tags;
this.title = title;
valid = true;
}
它有一个与之关联的Tag 数组。标签如下所示:
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private long activityId;
private String keyword;
public Tag(String keyword){
this.keyword = keyword;
activityId = -1;
}
Activity 和 Tag 都是实体,并且都有存储库:
public interface TagRepository extends CrudRepository<Tag, Long> {
}
public interface ActivityRepository extends CrudRepository<Activity, Long> {
}
有这个 create - 方法在数据库中存储了一个 Activity 对象:
@PostMapping
public Activity create(@RequestBody Activity input) {
if(input.getTags() != null) {
for(Tag t : input.getTags()) {
t.setActivityId(input.getId()); //associate which activity has which tags
tagRepository.save(t);
}
}
return activityRepository.save(input);
}
我通过 POST 传输这个 JSON:
{
"key":2,
"text":"Hello",
"tags":[{
"keyword":"h"
},{
"keyword":"b"
}
],
"title":"world"
}
但我收到此错误:
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.orm.jpa.JpaSystemException",
"message": "could not serialize; nested exception is org.hibernate.type.SerializationException: could not serialize"
两个类都有每个成员变量的 getter/setter,并且都有默认的 ctors,我只是没有在此处显示它们以使问题尽可能简短。
【问题讨论】:
-
用@OneToMany 注释标签属性?
-
我得到:
org.hibernate.AnnotationException: List/array has to be annotated with an @OrderColumn (or @IndexColumn): base.Activity.tags
标签: java json spring hibernate jpa