Skip to content

Commit 9201e71

Browse files
committed
HHH-19616 HHH-19614 add test
1 parent c01d67a commit 9201e71

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.inheritance;
6+
7+
import jakarta.persistence.Basic;
8+
import jakarta.persistence.DiscriminatorColumn;
9+
import jakarta.persistence.Entity;
10+
import jakarta.persistence.GeneratedValue;
11+
import jakarta.persistence.Id;
12+
import jakarta.persistence.ManyToOne;
13+
import jakarta.validation.constraints.NotNull;
14+
import org.hibernate.exception.ConstraintViolationException;
15+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
16+
import org.hibernate.testing.orm.junit.Jpa;
17+
import org.junit.jupiter.api.Test;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.fail;
21+
22+
@Jpa(annotatedClasses =
23+
{SingleTableConstraintsTest.Author.class,
24+
SingleTableConstraintsTest.Publisher.class,
25+
SingleTableConstraintsTest.Publication.class,
26+
SingleTableConstraintsTest.Journal.class,
27+
SingleTableConstraintsTest.Paper.class,
28+
SingleTableConstraintsTest.Monograph.class})
29+
class SingleTableConstraintsTest {
30+
@Test void test(EntityManagerFactoryScope scope) {
31+
scope.inTransaction( em -> {
32+
Monograph monograph = new Monograph();
33+
monograph.id = 1;
34+
monograph.title = "title";
35+
monograph.text = "text";
36+
monograph.pages = 10;
37+
monograph.edition = 1;
38+
Publisher publisher = new Publisher();
39+
publisher.name = "publisher";
40+
monograph.publisher = publisher;
41+
em.persist( publisher );
42+
em.persist( monograph );
43+
44+
Journal journal = new Journal();
45+
journal.id = 2;
46+
journal.title = "Journal Title";
47+
journal.text = "Journal Content";
48+
journal.pages = 100;
49+
Author editor = new Author();
50+
editor.ssn = "123-45-6789";
51+
editor.name = "John Editor";
52+
journal.editor = editor;
53+
em.persist( editor );
54+
em.persist( journal );
55+
56+
Paper paper = new Paper();
57+
paper.id = 3;
58+
paper.title = "Paper Title";
59+
paper.text = "Paper Content";
60+
paper.pages = 10;
61+
Author reviewer = new Author();
62+
reviewer.ssn = "987-65-4321";
63+
reviewer.name = "Jane Reviewer";
64+
paper.reviewer = reviewer;
65+
paper.journal = journal;
66+
em.persist( reviewer );
67+
em.persist( paper );
68+
} );
69+
70+
scope.inTransaction( em -> {
71+
try {
72+
em.createNativeQuery(
73+
"""
74+
insert into Publication (text, title, edition,\spublisher_id, type, id)
75+
values ('Lorem ipsum', 'Lorem Ipsum', 1, 1, 'Monograph', 5)
76+
""" )
77+
.executeUpdate();
78+
fail();
79+
}
80+
catch (ConstraintViolationException expected) {
81+
assertEquals( ConstraintViolationException.ConstraintKind.NOT_NULL, expected.getKind() );
82+
}
83+
} );
84+
scope.inTransaction( em -> {
85+
try {
86+
em.createNativeQuery(
87+
"""
88+
insert into Publication (pages, text, edition, publisher_id, type, id)
89+
values (20, 'Lorem ipsum', 1, 1, 'Monograph', 5)
90+
""" )
91+
.executeUpdate();
92+
fail();
93+
}
94+
catch (ConstraintViolationException expected) {
95+
assertEquals( ConstraintViolationException.ConstraintKind.NOT_NULL, expected.getKind() );
96+
}
97+
} );
98+
scope.inTransaction( em -> {
99+
try {
100+
em.createNativeQuery(
101+
"""
102+
insert into Publication (pages, text, title, publisher_id, type, id)
103+
values (100, 'Lorem ipsum', 'Lorem Ipsum', 1, 'Monograph', 5)
104+
""" )
105+
.executeUpdate();
106+
fail();
107+
}
108+
catch (ConstraintViolationException expected) {
109+
assertEquals( ConstraintViolationException.ConstraintKind.CHECK, expected.getKind() );
110+
}
111+
} );
112+
scope.inTransaction( em -> {
113+
try {
114+
em.createNativeQuery(
115+
"""
116+
insert into Publication (pages, text, title, edition, type, id)
117+
values (100, 'Lorem ipsum', 'Lorem Ipsum', 1, 'Monograph', 5)
118+
""" )
119+
.executeUpdate();
120+
fail();
121+
}
122+
catch (ConstraintViolationException expected) {
123+
assertEquals( ConstraintViolationException.ConstraintKind.CHECK, expected.getKind() );
124+
}
125+
} );
126+
scope.inTransaction( em -> {
127+
try {
128+
em.createNativeQuery(
129+
"""
130+
insert into Publication (pages, text, title, edition, publisher_id, type, id)
131+
values (100, 'Lorem ipsum', 'Lorem Ipsum', 1, 1, 'Shrubbery', 5)
132+
""" )
133+
.executeUpdate();
134+
fail();
135+
}
136+
catch (ConstraintViolationException expected) {
137+
assertEquals( ConstraintViolationException.ConstraintKind.CHECK, expected.getKind() );
138+
}
139+
} );
140+
scope.inTransaction( em -> {
141+
em.createNativeQuery(
142+
"""
143+
insert into Publication (pages, text, title, edition, publisher_id, type, id)
144+
values (100, 'Lorem ipsum', 'Lorem Ipsum', 1, 1, 'Monograph', 5)
145+
""" )
146+
.executeUpdate();
147+
} );
148+
}
149+
150+
@Entity(name = "Publication")
151+
@DiscriminatorColumn(name = "type")
152+
static abstract class Publication {
153+
@Id
154+
long id;
155+
@Basic(optional = false)
156+
String title;
157+
@Basic(optional = false)
158+
String text;
159+
int pages;
160+
}
161+
@Entity(name = "Monograph")
162+
static class Monograph extends Publication {
163+
@ManyToOne(optional = false)
164+
Publisher publisher;
165+
int edition;
166+
}
167+
@Entity(name = "Paper")
168+
static class Paper extends Publication {
169+
@ManyToOne(optional = false)
170+
Journal journal;
171+
@ManyToOne(optional = false)
172+
Author reviewer;
173+
}
174+
@Entity(name = "Journal")
175+
static class Journal extends Publication {
176+
@ManyToOne(optional = false)
177+
Author editor;
178+
}
179+
@Entity(name = "Author")
180+
static class Author {
181+
@Id
182+
String ssn;
183+
184+
@NotNull
185+
String name;
186+
}
187+
@Entity(name = "Publisher")
188+
static class Publisher {
189+
@Id
190+
@GeneratedValue
191+
long id;
192+
193+
@NotNull
194+
String name;
195+
}
196+
}

0 commit comments

Comments
 (0)