Skip to content

Commit 603e3ef

Browse files
committed
HHH-7045 Test entity as map key with equals/hashCode based on unique key
1 parent d0f98b7 commit 603e3ef

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.collection.map;
6+
7+
import jakarta.persistence.CascadeType;
8+
import jakarta.persistence.Column;
9+
import jakarta.persistence.Entity;
10+
import jakarta.persistence.Id;
11+
import jakarta.persistence.ManyToOne;
12+
import jakarta.persistence.MapKeyJoinColumn;
13+
import jakarta.persistence.OneToMany;
14+
import org.hibernate.testing.orm.junit.DomainModel;
15+
import org.hibernate.testing.orm.junit.Jira;
16+
import org.hibernate.testing.orm.junit.SessionFactory;
17+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
18+
import org.junit.jupiter.api.Test;
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import java.util.Objects;
23+
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
26+
@DomainModel(
27+
annotatedClasses = {
28+
EntityMapKeyWithUniqueKeyEqualsHashCodeTest.ContainerEntity.class, EntityMapKeyWithUniqueKeyEqualsHashCodeTest.KeyEntity.class, EntityMapKeyWithUniqueKeyEqualsHashCodeTest.ValueEntity.class
29+
}
30+
)
31+
@SessionFactory
32+
@Jira("https://hibernate.atlassian.net/browse/HHH-7045")
33+
public class EntityMapKeyWithUniqueKeyEqualsHashCodeTest {
34+
35+
@Test
36+
public void test(SessionFactoryScope scope) {
37+
scope.inTransaction(
38+
session -> {
39+
ContainerEntity ce = new ContainerEntity();
40+
ce.id = 1L;
41+
KeyEntity k1 = new KeyEntity();
42+
k1.id = 1L;
43+
k1.handle = "k1";
44+
KeyEntity k2 = new KeyEntity();
45+
k2.id = 2L;
46+
k2.handle = "k2";
47+
ValueEntity v1 = new ValueEntity();
48+
v1.id = 1L;
49+
v1.key = k1;
50+
v1.container = ce;
51+
ce.values.put( k1, v1 );
52+
ValueEntity v2 = new ValueEntity();
53+
v2.id = 2L;
54+
v2.key = k2;
55+
v2.container = ce;
56+
ce.values.put( k2, v2 );
57+
session.persist( ce );
58+
59+
}
60+
);
61+
scope.inTransaction(
62+
session -> {
63+
ContainerEntity ce = session.find( ContainerEntity.class, 1L );
64+
assertEquals( 2, ce.values.size() );
65+
}
66+
);
67+
68+
}
69+
70+
@Entity(name = "ContainerEntity")
71+
public static class ContainerEntity {
72+
@Id
73+
public Long id;
74+
@OneToMany(mappedBy = "container", cascade = CascadeType.ALL)
75+
@MapKeyJoinColumn(name = "key_id")
76+
public Map<KeyEntity, ValueEntity> values = new HashMap<>();
77+
}
78+
79+
@Entity(name = "KeyEntity")
80+
public static class KeyEntity {
81+
@Id
82+
public Long id;
83+
@Column(nullable = false, unique = true)
84+
public String handle;
85+
86+
@Override
87+
public boolean equals(Object o) {
88+
return o instanceof KeyEntity oke
89+
&& Objects.equals( handle, oke.handle );
90+
}
91+
92+
@Override
93+
public int hashCode() {
94+
return Objects.hashCode( handle );
95+
}
96+
}
97+
98+
@Entity(name = "ValueEntity")
99+
public static class ValueEntity {
100+
@Id
101+
public Long id;
102+
@ManyToOne(cascade = CascadeType.ALL)
103+
public KeyEntity key;
104+
@ManyToOne(cascade = CascadeType.ALL)
105+
public ContainerEntity container;
106+
107+
@Override
108+
public boolean equals(Object o) {
109+
return o instanceof ValueEntity ove
110+
&& Objects.equals( key, ove.key );
111+
}
112+
113+
@Override
114+
public int hashCode() {
115+
return Objects.hashCode( key );
116+
}
117+
}
118+
119+
}

0 commit comments

Comments
 (0)