Skip to content

Commit 31a4cbe

Browse files
blafondDavideD
authored andcommitted
[#710] Tests for @MapsId and @PrimaryKeyJoinColumn
1 parent 2cef2cb commit 31a4cbe

File tree

2 files changed

+197
-0
lines changed

2 files changed

+197
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: LGPL-2.1-or-later
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive;
7+
8+
import javax.persistence.Entity;
9+
import javax.persistence.Id;
10+
import javax.persistence.MapsId;
11+
import javax.persistence.OneToOne;
12+
13+
import org.hibernate.cfg.Configuration;
14+
import org.hibernate.reactive.stage.Stage;
15+
16+
import org.junit.After;
17+
import org.junit.Before;
18+
import org.junit.Test;
19+
20+
import io.vertx.ext.unit.TestContext;
21+
22+
public class OneToOneMapsIdTest extends BaseReactiveTest {
23+
24+
@Override
25+
protected Configuration constructConfiguration() {
26+
Configuration configuration = super.constructConfiguration();
27+
configuration.addAnnotatedClass( Person.class );
28+
configuration.addAnnotatedClass( PersonDetails.class );
29+
return configuration;
30+
}
31+
32+
@Before
33+
public void populateDb(TestContext context) {
34+
Person person = new Person("Joshua", 1);
35+
PersonDetails personDetails = new PersonDetails("Josh", person);
36+
37+
Stage.Session session = openSession();
38+
39+
test( context, session.persist( person, personDetails )
40+
.thenCompose( v -> session.flush() )
41+
);
42+
}
43+
44+
@After
45+
public void cleanDb(TestContext context) {
46+
test( context, deleteEntities( "PersonDetails", "Person" ) );
47+
}
48+
49+
@Test
50+
public void verifyParentIdIsSet(TestContext context) {
51+
test(
52+
context,
53+
openSession().find( PersonDetails.class, 1 )
54+
.thenAccept( foundPersonDetails ->
55+
context.assertNotNull( foundPersonDetails ) )
56+
);
57+
}
58+
59+
@Entity(name = "Person")
60+
public static class Person {
61+
@Id
62+
private Integer id;
63+
64+
private String name;
65+
66+
public Person() {}
67+
68+
public Person(String name, Integer id) {
69+
this.name = name;
70+
this.id = id;
71+
}
72+
}
73+
74+
@Entity(name = "PersonDetails")
75+
public static class PersonDetails {
76+
@Id
77+
private Integer id;
78+
79+
private String nickName;
80+
81+
@OneToOne
82+
@MapsId
83+
private Person person;
84+
85+
public PersonDetails(String nickName, Person person) {
86+
this.nickName = nickName;
87+
this.person = person;
88+
}
89+
90+
public PersonDetails() {}
91+
92+
public Person getPerson() {
93+
return person;
94+
}
95+
}
96+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: LGPL-2.1-or-later
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive;
7+
8+
import javax.persistence.Entity;
9+
import javax.persistence.Id;
10+
import javax.persistence.OneToOne;
11+
import javax.persistence.PrimaryKeyJoinColumn;
12+
13+
import org.hibernate.cfg.Configuration;
14+
import org.hibernate.reactive.stage.Stage;
15+
16+
import org.junit.After;
17+
import org.junit.Before;
18+
import org.junit.Test;
19+
20+
import io.vertx.ext.unit.TestContext;
21+
22+
public class OneToOnePrimaryKeyJoinColumnTest extends BaseReactiveTest {
23+
24+
@Override
25+
protected Configuration constructConfiguration() {
26+
Configuration configuration = super.constructConfiguration();
27+
configuration.addAnnotatedClass( Person.class );
28+
configuration.addAnnotatedClass( PersonDetails.class );
29+
return configuration;
30+
}
31+
32+
@Before
33+
public void populateDb(TestContext context) {
34+
Person person = new Person("Joshua", 1);
35+
PersonDetails personDetails = new PersonDetails("Josh", person);
36+
37+
Stage.Session session = openSession();
38+
39+
test( context, session.persist( person, personDetails )
40+
.thenCompose( v -> session.flush() )
41+
);
42+
}
43+
44+
@After
45+
public void cleanDb(TestContext context) {
46+
test( context, deleteEntities( "PersonDetails", "Person" ) );
47+
}
48+
49+
@Test
50+
public void verifyParentKeyIsSet(TestContext context) {
51+
test(
52+
context,
53+
openSession().find( PersonDetails.class, 1 )
54+
.thenAccept( foundPersonDetails ->
55+
context.assertNotNull( foundPersonDetails ) )
56+
);
57+
}
58+
59+
@Entity(name = "Person")
60+
public static class Person {
61+
@Id
62+
private Integer id;
63+
64+
private String name;
65+
66+
public Person() {}
67+
68+
public Person(String name, Integer id) {
69+
this.name = name;
70+
this.id = id;
71+
}
72+
73+
public Integer getId() {
74+
return id;
75+
}
76+
}
77+
78+
@Entity(name = "PersonDetails")
79+
public static class PersonDetails {
80+
@Id
81+
private Integer id;
82+
83+
private String nickName;
84+
85+
@OneToOne
86+
@PrimaryKeyJoinColumn
87+
private Person person;
88+
89+
public PersonDetails(String nickName, Person person) {
90+
this.nickName = nickName;
91+
this.person = person;
92+
this.id = person.getId();
93+
}
94+
95+
public PersonDetails() {}
96+
97+
public Person getPerson() {
98+
return person;
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)