Reputation: 964
I have trying spring rest with hibernate, JPA and JSON. I have to entities like below:
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
}
I have this dumy values in my db.
[
{
"id": 1,
"uniName": "uni1",
"students": [
{
"id": 1,
"stutName": "st1",
"university": {
"id": 1,
"uniName": "uni1"
}
},
{
"id": 2,
"stutName": "st2",
"university": {
"id": 1,
"uniName": "uni1"
}
}
]
}
]
first try: When I try to update the university with its student, there is no success and this is my hibernate log
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=?
second try: But when I Post same data for second time it is successful and the hibernate log is
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=?
Which is different from first one !!
What I am doing wrong in my hibernate annotations, or if I have missing something in my JSON annotation why the second try working.
UPDATE: this is my edit service.
@Override
@Transactional/*(propagation = Propagation.NESTED)*/
public T edit(T entity) throws Exception {
return entityManager.merge(entity);
}
any help and advice is appreciated.
Upvotes: 2
Views: 6841
Reputation: 964
after changing my entities like below it worked perfectly.
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")
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)
@JoinColumn(name = "university_id",updatable = false,insertable = false)
@JsonIgnoreProperties(value = "students", allowSetters = true)
private University university;
//setters and getters
}
I hope this help someone else that have same issue.
Upvotes: 1
Reputation: 12592
Try to update your entities like so
@Entity
@Table(name = "university")
public class University extends BaseEntity {
private String uniName;
@OneToMany(mappedBy = "university", ...) // Check the mappedBy property
private Collection<Student> students;
//setters and getters
}
@Entity
@Table(name = "student")
public class Student extends BaseEntity {
private String stuName;
@ManyToOne(fetch = FetchType.EAGER)
@JsonIgnoreProperties("students")
private University university; // university is bidirectionally mapped to Student
//setters and getters
}
Upvotes: 1