此网站上的所有类似问题都无法解决我的问题。
错误: org.hibernate.PersistentObjectException:传递给persist的分离实体:healthcheckapi.model.Checks
我正在尝试使用Hibernate(JPA)将两个对象持久化到MYSQL数据库。 “健康”与“支票”有一个关系。
代码:
健康类:
@Entity
@Table(name="health")
public class Health {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String server;
private String description;
private String timestamp;
private String offset;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "health")
private List<Checks> checks;
public Health() {
}
public Health(int id, String server, String description, String timestamp, String offset, List<Checks> checks) {
this.server = server;
this.description = description;
this.timestamp = timestamp;
this.offset = offset;
this.checks = checks;
}
检查班级:
@Entity
@Table(name="checks")
public class Checks {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private int status;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "healthid", referencedColumnName="id")
private Health health;
public Checks() {
}
public Checks(String name, int status) {
this.name = name;
this.status = status;
}
示例JSON运行状况对象:
{
"server": "Server18",
"description": "Fine",
"timestamp": "00:02:30",
"offset": "00:01:00",
"checks": [
{ "id": "1",
"name": "cpu",
"status": 90
},
{
"id": "2",
"name": "memory",
"status": 88
},
{
"id": "3",
"name": "apacheService",
"status": 76
}
]
}
请注意,两个对象的id都是自动生成的,我认为这是问题的一部分。
答案 0 :(得分:0)
使用gone
代替CascadeType.MERGE
。