Skip to content

Commit 4624cd6

Browse files
committed
HHH-16018 Showcase a way to select one or the other ToOne assocation.
1 parent f45e3db commit 4624cd6

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.hibernate.orm.test.query.hhh12225;
2+
3+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
4+
import org.hibernate.testing.orm.junit.JiraKey;
5+
import org.hibernate.testing.orm.junit.Jpa;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
10+
import jakarta.persistence.Entity;
11+
import jakarta.persistence.FetchType;
12+
import jakarta.persistence.GeneratedValue;
13+
import jakarta.persistence.GenerationType;
14+
import jakarta.persistence.Id;
15+
import jakarta.persistence.ManyToOne;
16+
17+
@Jpa(annotatedClasses = {
18+
CaseToOneAssociationTest.Subject.class,
19+
CaseToOneAssociationTest.Link.class
20+
})
21+
@JiraKey( "HHH-16018" )
22+
public class CaseToOneAssociationTest {
23+
24+
private Subject persistedFrom;
25+
26+
@BeforeAll
27+
public void setUp(EntityManagerFactoryScope scope) {
28+
persistedFrom = scope.fromTransaction( em -> {
29+
final Subject from = new Subject();
30+
final Subject to = new Subject();
31+
em.persist( from );
32+
em.persist( to );
33+
em.persist( new Link(from, to) );
34+
return from;
35+
} );
36+
}
37+
38+
@Test
39+
public void testUnsupportedCaseForEntity(EntityManagerFactoryScope scope) {
40+
// Won't catch AssertionError from assertFound, because it is Error, not Exception.
41+
// No test for message or more specific type, because there is nothing really related to the attempt
42+
// to select an entity in "case".
43+
Assertions.assertThrows( Exception.class, () -> {
44+
assertFound(
45+
scope.fromTransaction( em -> em
46+
.createQuery(
47+
"select case when l.from = :from then l.to else l.from end from Link l where from = :from",
48+
Subject.class
49+
)
50+
.setParameter( "from", persistedFrom )
51+
.getSingleResult() )
52+
);
53+
});
54+
}
55+
56+
private void assertFound(Subject found) {
57+
Assertions.assertNotEquals( persistedFrom.id, found.id, "Found itself" );
58+
}
59+
60+
@Test
61+
public void testSupportedCaseForJoin(EntityManagerFactoryScope scope) {
62+
assertFound(
63+
scope.fromTransaction( em -> em
64+
.createQuery(
65+
"select s from Link l join Subject s ON s.id = case when l.from = :from then l.to.id else l.from.id end where from = :from",
66+
Subject.class
67+
)
68+
.setParameter( "from", persistedFrom )
69+
.getSingleResult() )
70+
);
71+
}
72+
73+
@Entity(name = "Subject")
74+
public static class Subject {
75+
@Id
76+
@GeneratedValue(strategy = GenerationType.IDENTITY)
77+
public Long id;
78+
}
79+
80+
@Entity(name = "Link")
81+
public static class Link {
82+
@Id
83+
@GeneratedValue(strategy = GenerationType.IDENTITY)
84+
public Long id;
85+
@ManyToOne(fetch = FetchType.LAZY)
86+
public Subject from;
87+
@ManyToOne(fetch = FetchType.LAZY)
88+
public Subject to;
89+
90+
public Link() {}
91+
92+
public Link(Subject from, Subject to) {
93+
this.from = from;
94+
this.to = to;
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)