1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package
my.bean;
import
java.util.Set;
import
javax.persistence.CascadeType;
import
javax.persistence.Entity;
import
javax.persistence.GeneratedValue;
import
javax.persistence.Id;
import
javax.persistence.JoinColumn;
import
javax.persistence.JoinTable;
import
javax.persistence.ManyToMany;
import
javax.persistence.SequenceGenerator;
@Entity
public
class
Student {
private
int
id;
private
String sname;
private
Set
@Id
@SequenceGenerator
(name=
"idGenerator"
, sequenceName=
"seq_student_id"
)
@GeneratedValue
(generator=
"idGenerator"
)
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
public
String getSname() {
return
sname;
}
public
void
setSname(String sname) {
this
.sname = sname;
}
@ManyToMany
(cascade={CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.REFRESH})
@JoinTable
(name=
"student_course"
,
joinColumns={
@JoinColumn
(name=
"student_id"
)},
inverseJoinColumns={
@JoinColumn
(name=
"course_id"
)})
public
Set
return
courses;
}
public
void
setCourses(Set
this
.courses = courses;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package
my.bean;
import
java.util.Set;
import
javax.persistence.CascadeType;
import
javax.persistence.Entity;
import
javax.persistence.GeneratedValue;
import
javax.persistence.Id;
import
javax.persistence.ManyToMany;
import
javax.persistence.SequenceGenerator;
@Entity
public
class
Course {
private
int
id;
private
String cname;
private
Set
@Id
@SequenceGenerator
(name=
"idGenerator"
, sequenceName=
"seq_course_id"
)
@GeneratedValue
(generator=
"idGenerator"
)
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
public
String getCname() {
return
cname;
}
public
void
setCname(String cname) {
this
.cname = cname;
}
@ManyToMany
(cascade={CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.REFRESH}, mappedBy=
"courses"
)
public
Set
return
students;
}
public
void
setStudents(Set
this
.students = students;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package
my.test;
import
java.util.HashSet;
import
java.util.Set;
import
my.bean.Course;
import
my.bean.Student;
import
my.util.HibernateSessionFactory;
import
org.hibernate.HibernateException;
import
org.hibernate.Session;
import
org.hibernate.Transaction;
import
org.hibernate.cfg.Configuration;
import
org.hibernate.tool.hbm2ddl.SchemaExport;
public
class
Test {
public
static
void
main(String[] args) {
cascadeSave();
}
public
static
void
cascadeSave() {
Student s1 =
new
Student();
s1.setSname(
"zhangsan"
);
s1.setCourses(
new
HashSet
Course c1 =
new
Course();
c1.setCname(
"c++"
);
c1.setStudents(
new
HashSet
Course c2 =
new
Course();
c2.setCname(
"java"
);
c2.setStudents(
new
HashSet
Session session =
null
;
Transaction transaction =
null
;
try
{
session = HibernateSessionFactory.getSession();
transaction = session.beginTransaction();
s1.getCourses().add(c1);
s1.getCourses().add(c2);
session.save(s1);
//存储student对象时,无法自动存储级联的course对象,why?
transaction.commit();
}
catch
(HibernateException e) {
e.printStackTrace();
if
(transaction !=
null
) transaction.rollback();
}
finally
{
if
(session !=
null
) session.close();
}
}
}
解:@OneToMany(cascade = {CascadeType.PERSIST}),发现级联不起作用,如果更改为Hibernate的注解 @Cascade({org.hibernate.annotations.CascadeType.PERSIST}),依然不起作用,但改为 @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})之后,注解生效。
@ManyToOne(cascade = { CascadeType.PERSIST,CascadeType.MERGE})
I have three entities (taken from the Spring Data REST Exporter Example): Person, Address and Profile. A Person can have addresses and profiles.
In the client side I use Spring's RestTemplate. I added the Jackson2HalModule to the ObjectMapper used by the MappingJackson2HttpMessageConverter used by my RestTemplate. Since Address and Profile do not have references to other entities I can POST them to my Spring Data REST server, and they are successfully saved:
where But when I try to POST a Person instance
I get a Here is the actual body of the POST request:
I think it should be --> EDIT: It should be
in fact the response body from the server is
The possible solution I'd like to implementSince I can access the REST repositories from the client side I am looking for a way to customize the Jackson Json Serializer in order to:
I tried with Jackson's JsonSerializer and PropertyFilters for Address and Profile, but I want a serializer which serialize them as resource URI only when they are in an association. Any hint or aternative solution will be helpful. |