Skip to content

Commit 2896372

Browse files
dreab8Sanne
authored andcommitted
HHH-14257 Add test for issue
1 parent 00b3ccb commit 2896372

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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.collection.map;
8+
9+
import java.io.Serializable;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
import javax.persistence.Embeddable;
13+
import javax.persistence.EmbeddedId;
14+
import javax.persistence.Entity;
15+
import javax.persistence.Id;
16+
import javax.persistence.ManyToOne;
17+
import javax.persistence.MapKey;
18+
import javax.persistence.OneToMany;
19+
20+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
import static org.hamcrest.CoreMatchers.is;
25+
import static org.hamcrest.CoreMatchers.notNullValue;
26+
import static org.hamcrest.CoreMatchers.sameInstance;
27+
import static org.hamcrest.MatcherAssert.assertThat;
28+
29+
/**
30+
* @author Andrea Boriero
31+
*/
32+
public class EmbeddableIndexTest extends BaseCoreFunctionalTestCase {
33+
34+
@Override
35+
protected Class<?>[] getAnnotatedClasses() {
36+
return new Class[] { TheOne.class, TheMany.class };
37+
}
38+
39+
@Before
40+
public void setUp() {
41+
inTransaction(
42+
session -> {
43+
TheOne one = new TheOne( "1" );
44+
session.save( one );
45+
46+
TheMapKey theMapKey = new TheMapKey( one );
47+
TheMany theMany = new TheMany( theMapKey );
48+
session.save( theMany );
49+
50+
Map<TheMapKey, TheMany> map = new HashMap<>();
51+
map.put( theMapKey, theMany );
52+
one.setTheManys( map );
53+
}
54+
);
55+
}
56+
57+
@Test
58+
public void testIt() {
59+
inSession(
60+
session -> {
61+
TheOne one = session.get( TheOne.class, "1" );
62+
TheMapKey theMapKey = one.getTheManys().keySet().iterator().next();
63+
assertThat( theMapKey, is( notNullValue() ) );
64+
assertThat( theMapKey.getTheOne(), sameInstance( one ) );
65+
}
66+
);
67+
68+
69+
}
70+
71+
@Entity(name = "TheOne")
72+
public static class TheOne {
73+
private String id;
74+
private String aString;
75+
private Map<TheMapKey, TheMany> theManys = new HashMap<>();
76+
77+
TheOne() {
78+
}
79+
80+
public TheOne(String id) {
81+
this.id = id;
82+
}
83+
84+
@Id
85+
public String getId() {
86+
return this.id;
87+
}
88+
89+
public void setId(String id) {
90+
this.id = id;
91+
}
92+
93+
@OneToMany(mappedBy = "theMapKey.theOne")
94+
@MapKey(name = "theMapKey")
95+
public Map<TheMapKey, TheMany> getTheManys() {
96+
return theManys;
97+
}
98+
99+
public void setTheManys(Map<TheMapKey, TheMany> theManys) {
100+
this.theManys = theManys;
101+
}
102+
103+
public String getaString() {
104+
return aString;
105+
}
106+
107+
public void setaString(String aString) {
108+
this.aString = aString;
109+
}
110+
}
111+
112+
@Embeddable
113+
public static class TheMapKey implements Serializable {
114+
private TheOne theOne;
115+
private int anInt;
116+
117+
TheMapKey() {
118+
}
119+
120+
public TheMapKey(TheOne theOne) {
121+
this.theOne = theOne;
122+
}
123+
124+
@ManyToOne
125+
public TheOne getTheOne() {
126+
return theOne;
127+
}
128+
129+
public void setTheOne(TheOne theOne) {
130+
this.theOne = theOne;
131+
}
132+
133+
public int getAnInt() {
134+
return anInt;
135+
}
136+
137+
public void setAnInt(int anInt) {
138+
this.anInt = anInt;
139+
}
140+
}
141+
142+
@Entity(name = "TheMany")
143+
public static class TheMany {
144+
private TheMapKey theMapKey;
145+
private String aString;
146+
147+
TheMany() {
148+
}
149+
150+
public TheMany(TheMapKey theMapKey) {
151+
this.theMapKey = theMapKey;
152+
}
153+
154+
@EmbeddedId
155+
public TheMapKey getTheMapKey() {
156+
return theMapKey;
157+
}
158+
159+
public void setTheMapKey(TheMapKey theMapKey) {
160+
this.theMapKey = theMapKey;
161+
}
162+
163+
public String getaString() {
164+
return aString;
165+
}
166+
167+
public void setaString(String aString) {
168+
this.aString = aString;
169+
}
170+
}
171+
}

0 commit comments

Comments
 (0)