Skip to content

Commit 4a66035

Browse files
reda-alaouibeikov
authored andcommitted
HHH-18658 Inner join prevents finding an entity instance referencing an empty map
1 parent fc2ba3d commit 4a66035

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/PluralAttributeMappingImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ private TableGroup createOneToManyTableGroup(
950950
tableGroup,
951951
null,
952952
sqlAliasBase,
953-
SqlAstJoinType.INNER,
953+
joinType,
954954
fetched,
955955
false,
956956
creationState
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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.jpa;
8+
9+
import jakarta.persistence.CascadeType;
10+
import jakarta.persistence.Entity;
11+
import jakarta.persistence.FetchType;
12+
import jakarta.persistence.GeneratedValue;
13+
import jakarta.persistence.Id;
14+
import jakarta.persistence.JoinColumn;
15+
import jakarta.persistence.ManyToOne;
16+
import jakarta.persistence.MapKeyJoinColumn;
17+
import jakarta.persistence.OneToMany;
18+
import jakarta.persistence.OneToOne;
19+
import jakarta.persistence.Table;
20+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
21+
import org.hibernate.testing.orm.junit.JiraKey;
22+
import org.hibernate.testing.orm.junit.Jpa;
23+
import org.junit.jupiter.api.AfterEach;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
26+
27+
import java.util.HashMap;
28+
import java.util.Map;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* @author Réda Housni Alaoui
34+
*/
35+
@Jpa(annotatedClasses = {EmptyMapTest.User.class, EmptyMapTest.Identity.class, EmptyMapTest.UserIdentity.class})
36+
public class EmptyMapTest {
37+
38+
@BeforeEach
39+
public void setupData(EntityManagerFactoryScope scope) {
40+
scope.inTransaction( em -> em.persist( new User( 1 ) ) );
41+
}
42+
43+
@AfterEach
44+
public void cleanupData(EntityManagerFactoryScope scope) {
45+
scope.inTransaction(em -> em.createQuery( "delete from User u" ).executeUpdate() );
46+
}
47+
48+
@Test
49+
@JiraKey(value = "HHH-18658")
50+
public void test(EntityManagerFactoryScope scope) {
51+
scope.inTransaction( em -> {
52+
assertThat( em.find( User.class, 1 ) ).isNotNull();
53+
} );
54+
}
55+
56+
@Entity(name = "User")
57+
@Table(name = "user_tbl")
58+
public static class User {
59+
@Id
60+
private int id;
61+
private String name;
62+
63+
@OneToMany(
64+
mappedBy = "user",
65+
fetch = FetchType.EAGER,
66+
cascade = CascadeType.ALL,
67+
orphanRemoval = true)
68+
@MapKeyJoinColumn
69+
private final Map<Identity, UserIdentity> userIdentityByIdentity = new HashMap<>();
70+
71+
public User() {
72+
}
73+
74+
public User(int id) {
75+
this.id = id;
76+
}
77+
}
78+
79+
@Entity(name = "Identity")
80+
@Table(name = "identity_tbl")
81+
public static class Identity {
82+
@Id
83+
@GeneratedValue
84+
private int id;
85+
private String name;
86+
}
87+
88+
@Entity(name = "UserIdentity")
89+
@Table(name = "user_identity_tbl")
90+
public static class UserIdentity {
91+
@Id
92+
@GeneratedValue
93+
private int id;
94+
95+
@ManyToOne(fetch = FetchType.LAZY)
96+
@JoinColumn(nullable = false, updatable = false)
97+
private User user;
98+
99+
@OneToOne(fetch = FetchType.EAGER)
100+
@JoinColumn(nullable = false, updatable = false)
101+
private Identity identity;
102+
}
103+
104+
}

0 commit comments

Comments
 (0)