Skip to content

Commit 67177fa

Browse files
committed
HHH-17205 Add test for issue
1 parent 278ad6a commit 67177fa

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
6+
*/
7+
package org.hibernate.orm.test.annotations.idclass;
8+
9+
import java.io.Serializable;
10+
11+
import org.hibernate.testing.orm.junit.DomainModel;
12+
import org.hibernate.testing.orm.junit.Jira;
13+
import org.hibernate.testing.orm.junit.SessionFactory;
14+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
15+
import org.junit.jupiter.api.AfterAll;
16+
import org.junit.jupiter.api.BeforeAll;
17+
import org.junit.jupiter.api.Test;
18+
19+
import jakarta.persistence.Column;
20+
import jakarta.persistence.Entity;
21+
import jakarta.persistence.Id;
22+
import jakarta.persistence.IdClass;
23+
import jakarta.persistence.JoinColumn;
24+
import jakarta.persistence.OneToOne;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* @author Marco Belladelli
30+
*/
31+
@DomainModel( annotatedClasses = {
32+
IdClassOneToOneTest.CompositeId.class,
33+
IdClassOneToOneTest.ParentEntity.class,
34+
IdClassOneToOneTest.ChildEntity.class,
35+
} )
36+
@SessionFactory
37+
@Jira( "https://hibernate.atlassian.net/browse/HHH-17205" )
38+
public class IdClassOneToOneTest {
39+
@BeforeAll
40+
public void setUp(SessionFactoryScope scope) {
41+
scope.inTransaction( session -> {
42+
session.persist( new ParentEntity( "1", "one" ) );
43+
session.persist( new ChildEntity( "1", "one" ) );
44+
} );
45+
}
46+
47+
@AfterAll
48+
public void tearDown(SessionFactoryScope scope) {
49+
scope.inTransaction( session -> {
50+
session.createMutationQuery( "delete from ChildEntity" ).executeUpdate();
51+
session.createMutationQuery( "delete from ParentEntity" ).executeUpdate();
52+
} );
53+
}
54+
55+
@Test
56+
public void testQuery(SessionFactoryScope scope) {
57+
scope.inTransaction( session -> {
58+
final ParentEntity result = session.createQuery(
59+
"select p from ParentEntity p where p.first = :first",
60+
ParentEntity.class
61+
).setParameter( "first", "1" ).getSingleResult();
62+
assertThat( result.getChildEntity() ).isNotNull();
63+
assertThat( result.getChildEntity().getFirst() ).isEqualTo( "1" );
64+
assertThat( result.getChildEntity().getSecond() ).isEqualTo( "one" );
65+
} );
66+
}
67+
68+
@Test
69+
public void testFind(SessionFactoryScope scope) {
70+
scope.inTransaction( session -> {
71+
final ParentEntity result = session.find( ParentEntity.class, new CompositeId( "1", "one" ) );
72+
assertThat( result.getChildEntity() ).isNotNull();
73+
assertThat( result.getChildEntity().getFirst() ).isEqualTo( "1" );
74+
assertThat( result.getChildEntity().getSecond() ).isEqualTo( "one" );
75+
} );
76+
}
77+
78+
public static class CompositeId implements Serializable {
79+
private String first;
80+
private String second;
81+
82+
public CompositeId() {
83+
}
84+
85+
public CompositeId(String first, String second) {
86+
this.first = first;
87+
this.second = second;
88+
}
89+
}
90+
91+
@Entity( name = "ParentEntity" )
92+
@IdClass( CompositeId.class )
93+
public static class ParentEntity {
94+
@Id
95+
@Column( name = "col1" )
96+
private String first;
97+
98+
@Id
99+
@Column( name = "col2" )
100+
private String second;
101+
102+
@OneToOne( mappedBy = "parentEntity" )
103+
private ChildEntity childEntity;
104+
105+
public ParentEntity() {
106+
}
107+
108+
public ParentEntity(String first, String second) {
109+
this.first = first;
110+
this.second = second;
111+
}
112+
113+
public ChildEntity getChildEntity() {
114+
return childEntity;
115+
}
116+
}
117+
118+
@Entity( name = "ChildEntity" )
119+
@IdClass( CompositeId.class )
120+
public static class ChildEntity {
121+
@Id
122+
@Column( name = "col1" )
123+
private String first;
124+
125+
@Id
126+
@Column( name = "col2" )
127+
private String second;
128+
129+
@OneToOne
130+
@JoinColumn( name = "col1", referencedColumnName = "col1" )
131+
@JoinColumn( name = "col2", referencedColumnName = "col2" )
132+
private ParentEntity parentEntity;
133+
134+
public ChildEntity() {
135+
}
136+
137+
public ChildEntity(String first, String second) {
138+
this.first = first;
139+
this.second = second;
140+
}
141+
142+
public String getFirst() {
143+
return first;
144+
}
145+
146+
public String getSecond() {
147+
return second;
148+
}
149+
150+
public ParentEntity getParentEntity() {
151+
return parentEntity;
152+
}
153+
}
154+
}

0 commit comments

Comments
 (0)