Skip to content

Commit 67af47a

Browse files
mbelladebeikov
authored andcommitted
HHH-17491 Add test for issue
1 parent 7e72a43 commit 67af47a

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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.query;
8+
9+
import java.util.List;
10+
11+
import org.hibernate.testing.orm.domain.gambit.BasicEntity;
12+
import org.hibernate.testing.orm.junit.DomainModel;
13+
import org.hibernate.testing.orm.junit.Jira;
14+
import org.hibernate.testing.orm.junit.SessionFactory;
15+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
16+
import org.junit.jupiter.api.AfterAll;
17+
import org.junit.jupiter.api.BeforeAll;
18+
import org.junit.jupiter.api.Test;
19+
20+
import jakarta.persistence.Entity;
21+
import jakarta.persistence.Id;
22+
import jakarta.persistence.ManyToOne;
23+
import jakarta.persistence.MappedSuperclass;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.assertj.core.api.Assertions.fail;
27+
28+
/**
29+
* @author Marco Belladelli
30+
*/
31+
@DomainModel( annotatedClasses = {
32+
BasicEntity.class,
33+
MappedSuperclassAttributeInMultipleSubtypesTest.BaseEntity.class,
34+
MappedSuperclassAttributeInMultipleSubtypesTest.MappedSuper.class,
35+
MappedSuperclassAttributeInMultipleSubtypesTest.ChildOne.class,
36+
MappedSuperclassAttributeInMultipleSubtypesTest.ChildTwo.class,
37+
} )
38+
@SessionFactory
39+
@Jira( "https://hibernate.atlassian.net/browse/HHH-17491" )
40+
public class MappedSuperclassAttributeInMultipleSubtypesTest {
41+
@BeforeAll
42+
public void setUp(SessionFactoryScope scope) {
43+
scope.inTransaction( session -> {
44+
final BasicEntity basicEntity = new BasicEntity( 1, "basic" );
45+
session.persist( basicEntity );
46+
session.persist( new ChildOne( 1L, "test", 1, basicEntity ) );
47+
session.persist( new ChildTwo( 2L, "test", 1D, basicEntity ) );
48+
} );
49+
}
50+
51+
@AfterAll
52+
public void tearDown(SessionFactoryScope scope) {
53+
scope.inTransaction( session -> {
54+
session.createMutationQuery( "delete from BaseEntity" ).executeUpdate();
55+
session.createMutationQuery( "delete from BasicEntity" ).executeUpdate();
56+
} );
57+
}
58+
59+
@Test
60+
public void testSameTypeAttribute(SessionFactoryScope scope) {
61+
scope.inTransaction( session -> {
62+
final List<BaseEntity> resultList = session.createQuery(
63+
"from BaseEntity e where e.stringProp = 'test'",
64+
BaseEntity.class
65+
).getResultList();
66+
assertThat( resultList ).hasSize( 2 );
67+
assertThat( resultList.stream().map( BaseEntity::getId ) ).contains( 1L, 2L );
68+
} );
69+
}
70+
71+
@Test
72+
public void testDifferentTypeAttribute(SessionFactoryScope scope) {
73+
scope.inTransaction( session -> {
74+
try {
75+
session.createQuery(
76+
"from BaseEntity e where e.otherProp = 1",
77+
BaseEntity.class
78+
).getResultList();
79+
fail( "This shouldn't work since the attribute is defined with different types" );
80+
}
81+
catch (Exception e) {
82+
final Throwable cause = e.getCause().getCause();
83+
assertThat( cause ).isInstanceOf( IllegalArgumentException.class );
84+
assertThat( cause.getMessage() ).contains( "Could not resolve attribute 'otherProp'" );
85+
}
86+
} );
87+
}
88+
89+
@Test
90+
public void testToOneAttribute(SessionFactoryScope scope) {
91+
scope.inTransaction( session -> {
92+
final BasicEntity basicEntity = session.find( BasicEntity.class, 1 );
93+
final List<BaseEntity> resultList = session.createQuery(
94+
"from BaseEntity e where e.toOneProp = :be",
95+
BaseEntity.class
96+
).setParameter( "be", basicEntity ).getResultList();
97+
assertThat( resultList ).hasSize( 2 );
98+
assertThat( resultList.stream().map( BaseEntity::getId ) ).contains( 1L, 2L );
99+
} );
100+
}
101+
102+
@Entity( name = "BaseEntity" )
103+
public static class BaseEntity {
104+
@Id
105+
private Long id;
106+
107+
public BaseEntity() {
108+
}
109+
110+
public BaseEntity(Long id) {
111+
this.id = id;
112+
}
113+
114+
public Long getId() {
115+
return id;
116+
}
117+
}
118+
119+
@MappedSuperclass
120+
@SuppressWarnings( "unused" )
121+
public static abstract class MappedSuper extends BaseEntity {
122+
private String stringProp;
123+
124+
private Integer otherProp;
125+
126+
@ManyToOne
127+
private BasicEntity toOneProp;
128+
129+
public MappedSuper() {
130+
}
131+
132+
public MappedSuper(Long id, String stringProp, Integer otherProp, BasicEntity toOneProp) {
133+
super( id );
134+
this.stringProp = stringProp;
135+
this.otherProp = otherProp;
136+
this.toOneProp = toOneProp;
137+
}
138+
}
139+
140+
@Entity( name = "ChildOne" )
141+
public static class ChildOne extends MappedSuper {
142+
public ChildOne() {
143+
}
144+
145+
public ChildOne(Long id, String stringProp, Integer otherProp, BasicEntity toOneProp) {
146+
super( id, stringProp, otherProp, toOneProp );
147+
}
148+
}
149+
150+
@Entity( name = "ChildTwo" )
151+
@SuppressWarnings( "unused" )
152+
public static class ChildTwo extends BaseEntity {
153+
private String stringProp;
154+
155+
private Double otherProp;
156+
157+
@ManyToOne
158+
private BasicEntity toOneProp;
159+
160+
public ChildTwo() {
161+
}
162+
163+
public ChildTwo(Long id, String stringProp, Double otherProp, BasicEntity toOneProp) {
164+
super( id );
165+
this.stringProp = stringProp;
166+
this.otherProp = otherProp;
167+
this.toOneProp = toOneProp;
168+
}
169+
}
170+
}

0 commit comments

Comments
 (0)