我試着用休眠,JPA和JSON來休眠。 我不得不實體象下面這樣:JPA和Hibernate dosnt更新一到多關係在第一次嘗試中使用JSON的春天休息
University.java
@Entity()
@Table(name = "university")
public class University extends BaseEntity {
private String uniName;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinColumn(name = "university_id", foreignKey = @ForeignKey(name = "university_id"))
private Collection<Student> students;
//setters and getters
}
Student.java
@Entity
@Table(name = "student")
public class Student extends BaseEntity {
private String stuName;
@ManyToOne(fetch = FetchType.EAGER)
@JsonIgnoreProperties("students")
private University university;
//setters and getters
}
我在我的分貝這dumy值。
[
{
"id": 1,
"uniName": "uni1",
"students": [
{
"id": 1,
"stutName": "st1",
"university": {
"id": 1,
"uniName": "uni1"
}
},
{
"id": 2,
"stutName": "st2",
"university": {
"id": 1,
"uniName": "uni1"
}
}
]
}
]
第一次嘗試:當我嘗試更新它的學生的大學,沒有成功 ,這是我的休眠日誌
Hibernate:
select
university0_.id as id1_5_1_,
university0_.uniName as uniName2_5_1_,
students1_.university_id as universi3_4_3_,
students1_.id as id1_4_3_,
students1_.id as id1_4_0_,
students1_.stuName as stuName2_4_0_,
students1_.university_id as universi3_4_0_
from
university university0_
left outer join
student students1_
on university0_.id=students1_.university_id
where
university0_.id=?
Hibernate:
update
student
set
stuName=?,
university_id=?
where
id=?
Hibernate:
update
university
set
uniName=?
where
id=?
Hibernate:
update
student
set
stuName=?,
university_id=?
where
id=?
第二次嘗試:但是,當我發表相同的數據第二次它是成功和休眠日誌是
Hibernate:
select
university0_.id as id1_5_1_,
university0_.uniName as uniName2_5_1_,
students1_.university_id as universi3_4_3_,
students1_.id as id1_4_3_,
students1_.id as id1_4_0_,
students1_.stuName as stuName2_4_0_,
students1_.university_id as universi3_4_0_
from
university university0_
left outer join
student students1_
on university0_.id=students1_.university_id
where
university0_.id=?
Hibernate:
select
student0_.id as id1_4_0_,
student0_.stuName as stuName2_4_0_,
student0_.university_id as universi3_4_0_
from
student student0_
where
student0_.id=?
Hibernate:
select
student0_.id as id1_4_0_,
student0_.stuName as stuName2_4_0_,
student0_.university_id as universi3_4_0_
from
student student0_
where
student0_.id=?
Hibernate:
update
student
set
university_id=?
where
id=?
Hibernate:
update
student
set
university_id=?
where
id=?
與第一個不同!
我在做什麼我的hibernate註釋錯誤,或者如果我在我的JSON註釋中缺少某些東西,爲什麼第二次嘗試工作。
更新:這是我的編輯服務。
@Override
@Transactional/*(propagation = Propagation.NESTED)*/
public T edit(T entity) throws Exception {
return entityManager.merge(entity);
}
任何幫助和建議表示讚賞。
發佈持久數據的代碼。 –
@ Abdullah Khan我更新了我的問題 – Generic
你的映射是錯誤的。閱讀Hibernate手冊,瞭解如何正確映射雙向OneToMany關聯。 –