Skip to content

Commit 72842e5

Browse files
The-Huginnbeikov
authored andcommitted
[HHH-17288] Create test with OneToOne relation
1 parent ed84edd commit 72842e5

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package org.hibernate.orm.test.idclass;
2+
3+
import jakarta.persistence.CascadeType;
4+
import jakarta.persistence.Entity;
5+
import jakarta.persistence.FetchType;
6+
import jakarta.persistence.GeneratedValue;
7+
import jakarta.persistence.Id;
8+
import jakarta.persistence.IdClass;
9+
import jakarta.persistence.JoinColumn;
10+
import jakarta.persistence.OneToOne;
11+
import org.hibernate.annotations.OnDelete;
12+
import org.hibernate.annotations.OnDeleteAction;
13+
import org.hibernate.testing.orm.junit.DomainModel;
14+
import org.hibernate.testing.orm.junit.SessionFactory;
15+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
16+
import org.junit.jupiter.api.BeforeEach;
17+
import org.junit.jupiter.api.Test;
18+
19+
import java.io.Serializable;
20+
import java.util.Objects;
21+
22+
import static org.hamcrest.CoreMatchers.is;
23+
import static org.hamcrest.MatcherAssert.assertThat;
24+
25+
@DomainModel(
26+
annotatedClasses = {
27+
IdClassWithOneToOneTest.Task.class,
28+
IdClassWithOneToOneTest.TaskText.class
29+
}
30+
)
31+
@SessionFactory(useCollectingStatementInspector = true)
32+
public class IdClassWithOneToOneTest {
33+
34+
@BeforeEach
35+
void setup(SessionFactoryScope scope) {
36+
scope.inTransaction(
37+
session -> {
38+
Task task = new Task(new TaskText("en", "Localized in en"));
39+
session.save( task );
40+
}
41+
);
42+
}
43+
44+
@Test
45+
public void testCreate(SessionFactoryScope scope) {
46+
scope.inTransaction(
47+
session -> {
48+
Task task = session.get(Task.class, 1L);
49+
assertThat( task.text.content, is( "Localized in en" ) );
50+
assertThat( task.text.locale, is( "en" ) );
51+
}
52+
);
53+
}
54+
55+
@Entity
56+
public static class Task {
57+
58+
@Id
59+
@GeneratedValue
60+
public Long id;
61+
62+
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "task")
63+
public TaskText text;
64+
65+
public Task() {}
66+
67+
public Task(TaskText text) {
68+
this.text = text;
69+
text.task = this;
70+
}
71+
}
72+
73+
@Entity
74+
@IdClass(TaskText.TaskTextPK.class)
75+
public static class TaskText {
76+
77+
public static class TaskTextPK implements Serializable {
78+
public Long task;
79+
public String locale;
80+
81+
public TaskTextPK() {}
82+
83+
public TaskTextPK(Long task, String locale) {
84+
this.task = task;
85+
this.locale = locale;
86+
}
87+
88+
@Override
89+
public boolean equals(Object obj) {
90+
if (!(obj instanceof TaskTextPK)) {
91+
return false;
92+
} else {
93+
TaskTextPK pk = (TaskTextPK) obj;
94+
return Objects.equals(task, pk.task) && locale.equals(pk.locale);
95+
}
96+
}
97+
98+
@Override
99+
public int hashCode() {
100+
return task.hashCode() + locale.hashCode();
101+
}
102+
}
103+
104+
public TaskText() {}
105+
106+
public TaskText(String locale, String content) {
107+
this.locale = locale;
108+
this.content = content;
109+
}
110+
111+
@Id
112+
@OneToOne(fetch = FetchType.EAGER)
113+
@OnDelete(action = OnDeleteAction.CASCADE)
114+
@JoinColumn(name = "id")
115+
public Task task;
116+
117+
@Id
118+
public String locale;
119+
120+
public String content;
121+
}
122+
}

0 commit comments

Comments
 (0)