Skip to content

Commit 5c924a2

Browse files
committed
HHH-9952 Add test for issue
1 parent a90aaa4 commit 5c924a2

File tree

5 files changed

+435
-0
lines changed

5 files changed

+435
-0
lines changed
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
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.test.formulajoin;
8+
9+
import org.hibernate.Hibernate;
10+
11+
import org.hibernate.testing.TestForIssue;
12+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
13+
import org.junit.Before;
14+
import org.junit.Ignore;
15+
import org.junit.Test;
16+
17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertFalse;
19+
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertNull;
21+
import static org.junit.Assert.assertTrue;
22+
23+
@TestForIssue(jiraKey = "HHH-9952")
24+
public class AssociationFormulaTest extends BaseCoreFunctionalTestCase {
25+
26+
public AssociationFormulaTest() {
27+
super();
28+
}
29+
30+
@Override
31+
protected boolean isCleanupTestDataRequired() {
32+
return true;
33+
}
34+
35+
@Override
36+
protected boolean rebuildSessionFactoryOnError() {
37+
return true;
38+
}
39+
40+
@Override
41+
public String[] getMappings() {
42+
return new String[] { "formulajoin/Mapping.hbm.xml" };
43+
}
44+
45+
@Before
46+
public void fillDb() {
47+
48+
Entity entity = new Entity();
49+
entity.setId( new Id( "test", 1 ) );
50+
entity.setOther( new OtherEntity() );
51+
entity.getOther().setId( new Id( "test", 2 ) );
52+
53+
Entity otherNull = new Entity();
54+
otherNull.setId( new Id( "null", 3 ) );
55+
56+
inTransaction(
57+
session -> {
58+
session.merge( entity );
59+
session.merge( otherNull );
60+
}
61+
);
62+
}
63+
64+
@Test
65+
public void testJoin() {
66+
67+
inTransaction(
68+
session -> {
69+
Entity loaded = (Entity) session.createQuery( "select e from Entity e inner join e.other o" )
70+
.uniqueResult();
71+
assertNotNull( "loaded", loaded );
72+
assertEquals( 1, loaded.getId().getId() );
73+
assertEquals( 2, loaded.getOther().getId().getId() );
74+
assertFalse( Hibernate.isInitialized( loaded.getOther() ) );
75+
Hibernate.initialize( loaded.getOther() );
76+
}
77+
);
78+
}
79+
80+
@Test
81+
public void testJoinFetch() {
82+
inTransaction(
83+
session -> {
84+
Entity loaded = (Entity) session.createQuery( "select e from Entity e inner join fetch e.other o" )
85+
.uniqueResult();
86+
assertNotNull( "loaded", loaded );
87+
assertEquals( 1, loaded.getId().getId() );
88+
assertTrue( Hibernate.isInitialized( loaded.getOther() ) );
89+
assertEquals( 2, loaded.getOther().getId().getId() );
90+
}
91+
);
92+
}
93+
94+
@Test
95+
public void testSelectFullNull() {
96+
inTransaction(
97+
session -> {
98+
Entity loaded = (Entity) session.createQuery( "from Entity e where e.other is null" )
99+
.uniqueResult();
100+
assertNotNull( "loaded", loaded );
101+
assertEquals( 3, loaded.getId().getId() );
102+
assertNull( loaded.getOther() );
103+
104+
}
105+
);
106+
}
107+
108+
@Test
109+
public void testSelectPartialNull() {
110+
inTransaction(
111+
session -> {
112+
Entity loaded = (Entity) session.createQuery( "from Entity e where e.other.id.id is null" )
113+
.uniqueResult();
114+
assertNotNull( "loaded", loaded );
115+
assertEquals( 3, loaded.getId().getId() );
116+
assertNull( loaded.getOther() );
117+
}
118+
);
119+
}
120+
121+
@Test
122+
public void testSelectFull() {
123+
inTransaction(
124+
session -> {
125+
OtherEntity other = new OtherEntity();
126+
other.setId( new Id( "test", 2 ) );
127+
Entity loaded = (Entity) session.createQuery( "from Entity e where e.other = :other" )
128+
.setParameter( "other", other )
129+
.uniqueResult();
130+
assertNotNull( "loaded", loaded );
131+
assertEquals( 1, loaded.getId().getId() );
132+
assertNotNull( loaded.getOther() );
133+
assertEquals( 2, loaded.getOther().getId().getId() );
134+
}
135+
);
136+
}
137+
138+
@Test
139+
public void testUpdateFromExisting() {
140+
inTransaction(
141+
session -> {
142+
Entity loaded = (Entity) session.createQuery( "from Entity e where e.id.id = 1" ).uniqueResult();
143+
assertNotNull( "loaded", loaded );
144+
assertNotNull( loaded.getOther() );
145+
loaded.setOther( new OtherEntity() );
146+
loaded.getOther().setId( new Id( "test", 3 ) );
147+
}
148+
);
149+
}
150+
151+
@Test
152+
public void testUpdateFromNull() {
153+
inTransaction(
154+
session -> {
155+
Entity loaded = (Entity) session.createQuery( "from Entity e where e.id.id = 3" ).uniqueResult();
156+
assertNotNull( "loaded", loaded );
157+
assertNull( loaded.getOther() );
158+
loaded.setOther( new OtherEntity() );
159+
loaded.getOther().setId( new Id( "test", 3 ) );
160+
}
161+
);
162+
}
163+
164+
@Test
165+
@Ignore("multi-column updates don't work!")
166+
public void testUpdateHql() {
167+
inTransaction(
168+
session -> {
169+
OtherEntity other = new OtherEntity();
170+
other.setId( new Id( "null", 4 ) );
171+
assertEquals(
172+
"execute",
173+
1,
174+
session.createQuery( "update Entity e set e.other = :other where e.id.id = 3" )
175+
.setParameter( "other", other )
176+
.executeUpdate()
177+
);
178+
Entity loaded = (Entity) session.createQuery( "from Entity e where e.id.id = 3" ).uniqueResult();
179+
assertNotNull( "loaded", loaded );
180+
assertEquals( 4, loaded.getOther().getId().getId() );
181+
}
182+
);
183+
}
184+
185+
@Test
186+
@Ignore("multi-column updates don't work!")
187+
public void testUpdateHqlNull() {
188+
inTransaction(
189+
session -> {
190+
assertEquals(
191+
"execute",
192+
1,
193+
session.createQuery( "update Entity e set e.other = null where e.id.id = 1" )
194+
.executeUpdate()
195+
);
196+
Entity loaded = (Entity) session.createQuery( "from Entity e where e.id.id = 1" ).uniqueResult();
197+
assertNotNull( "loaded", loaded );
198+
assertEquals( 4, loaded.getOther().getId().getId() );
199+
}
200+
);
201+
}
202+
203+
@Test
204+
public void testDeleteHql() {
205+
inTransaction(
206+
session -> {
207+
OtherEntity other = new OtherEntity();
208+
other.setId( new Id( "test", 2 ) );
209+
assertEquals(
210+
"execute",
211+
1,
212+
session.createQuery( "delete Entity e where e.other = :other" )
213+
.setParameter( "other", other )
214+
.executeUpdate()
215+
);
216+
Entity loaded = (Entity) session.createQuery( "from Entity e where e.id.id = 1" ).uniqueResult();
217+
assertNull( "loaded", loaded );
218+
}
219+
);
220+
}
221+
222+
@Test
223+
public void testDeleteHqlNull() {
224+
inTransaction(
225+
session -> {
226+
assertEquals(
227+
"execute",
228+
1,
229+
session.createQuery( "delete Entity e where e.other is null" ).executeUpdate()
230+
);
231+
Entity loaded = (Entity) session.createQuery( "from Entity e where e.id.id = 3" ).uniqueResult();
232+
assertNull( "loaded", loaded );
233+
}
234+
);
235+
}
236+
237+
@Test
238+
public void testPersist() {
239+
inTransaction(
240+
session -> {
241+
Entity entity = new Entity();
242+
entity.setId( new Id( "new", 5 ) );
243+
entity.setOther( new OtherEntity() );
244+
entity.getOther().setId( new Id( "new", 6 ) );
245+
session.persist( entity );
246+
}
247+
);
248+
249+
inTransaction(
250+
session -> {
251+
Entity loaded = (Entity) session.createQuery( "from Entity e where e.id.id = 5" ).uniqueResult();
252+
assertNotNull( "loaded", loaded );
253+
assertEquals( 6, loaded.getOther().getId().getId() );
254+
}
255+
);
256+
}
257+
258+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.test.formulajoin;
8+
9+
public class Entity {
10+
private Id id;
11+
12+
private String name;
13+
14+
private OtherEntity other;
15+
16+
public Id getId() {
17+
return id;
18+
}
19+
20+
public void setId(Id id) {
21+
this.id = id;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public void setName(String name) {
29+
this.name = name;
30+
}
31+
32+
public OtherEntity getOther() {
33+
return other;
34+
}
35+
36+
public void setOther(OtherEntity partial) {
37+
this.other = partial;
38+
}
39+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.test.formulajoin;
8+
9+
import java.io.Serializable;
10+
11+
public class Id implements Serializable {
12+
private String mandant;
13+
14+
private int id;
15+
16+
public Id() {
17+
super();
18+
}
19+
20+
public Id(String mandant, int id) {
21+
super();
22+
this.mandant = mandant;
23+
this.id = id;
24+
}
25+
26+
public String getMandant() {
27+
return mandant;
28+
}
29+
30+
public void setMandant(String mandant) {
31+
this.mandant = mandant;
32+
}
33+
34+
public int getId() {
35+
return id;
36+
}
37+
38+
public void setId(int id) {
39+
this.id = id;
40+
}
41+
42+
@Override
43+
public int hashCode() {
44+
final int prime = 31;
45+
int result = 1;
46+
result = prime * result + id;
47+
result = prime * result + ( ( mandant == null ) ? 0 : mandant.hashCode() );
48+
return result;
49+
}
50+
51+
@Override
52+
public boolean equals(Object obj) {
53+
if ( this == obj ) {
54+
return true;
55+
}
56+
if ( obj == null ) {
57+
return false;
58+
}
59+
if ( getClass() != obj.getClass() ) {
60+
return false;
61+
}
62+
Id other = (Id) obj;
63+
if ( id != other.id ) {
64+
return false;
65+
}
66+
if ( mandant == null ) {
67+
if ( other.mandant != null ) {
68+
return false;
69+
}
70+
}
71+
else if ( !mandant.equals( other.mandant ) ) {
72+
return false;
73+
}
74+
return true;
75+
}
76+
}

0 commit comments

Comments
 (0)