Skip to content

Commit 8557c5c

Browse files
mbelladebeikov
authored andcommitted
HHH-17925 Add test for issue
1 parent 758a660 commit 8557c5c

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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.mapping.manytomany;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
import org.hibernate.Hibernate;
13+
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.AfterAll;
19+
import org.junit.jupiter.api.BeforeAll;
20+
import org.junit.jupiter.api.Test;
21+
22+
import jakarta.persistence.CascadeType;
23+
import jakarta.persistence.Entity;
24+
import jakarta.persistence.Id;
25+
import jakarta.persistence.JoinColumn;
26+
import jakarta.persistence.JoinTable;
27+
import jakarta.persistence.ManyToMany;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* @author Marco Belladelli
33+
*/
34+
@DomainModel( annotatedClasses = {
35+
ManyToManyNonAggregatedIdJoinColumnTest.EntityA.class,
36+
ManyToManyNonAggregatedIdJoinColumnTest.EntityB.class,
37+
} )
38+
@SessionFactory
39+
@Jira( "https://hibernate.atlassian.net/browse/HHH-17925" )
40+
public class ManyToManyNonAggregatedIdJoinColumnTest {
41+
@Test
42+
public void testMapping(SessionFactoryScope scope) {
43+
scope.inSession( session -> {
44+
final EntityA entityA = session.createQuery( "from EntityA where id = 1", EntityA.class ).getSingleResult();
45+
assertThat( entityA.getEntityBList() ).hasSize( 2 )
46+
.extracting( EntityB::getName )
47+
.containsOnly( "entityb_1", "entityb_2" );
48+
assertThat( session.createQuery( "from EntityA where id = 2", EntityA.class ).getSingleResult().getEntityBList() ).isEmpty();
49+
} );
50+
}
51+
52+
@Test
53+
public void testQueryFunctions(SessionFactoryScope scope) {
54+
scope.inSession( session -> {
55+
final EntityA entityA = session.createQuery(
56+
"from EntityA e join fetch e.entityBList where e.id = 1",
57+
EntityA.class
58+
).getSingleResult();
59+
assertThat( entityA.getEntityBList() ).matches(
60+
Hibernate::isInitialized,
61+
"Expected join-fetched list to be initialized"
62+
)
63+
.hasSize( 2 )
64+
.extracting( EntityB::getName )
65+
.containsOnly( "entityb_1", "entityb_2" );
66+
} );
67+
scope.inSession( session -> {
68+
assertThat( session.createQuery(
69+
"select size(e.entityBList) from EntityA e where e.id = 2",
70+
Integer.class
71+
).getSingleResult() ).isEqualTo( 0 );
72+
} );
73+
}
74+
75+
@BeforeAll
76+
public void setUp(SessionFactoryScope scope) {
77+
scope.inTransaction( session -> {
78+
final EntityA entityA = new EntityA( 1L, "entitya_1" );
79+
entityA.getEntityBList().add( new EntityB( 1L, "entityb_1" ) );
80+
entityA.getEntityBList().add( new EntityB( 2L, "entityb_2" ) );
81+
session.persist( entityA );
82+
session.persist( new EntityA( 2L, "entitya_2" ) );
83+
} );
84+
}
85+
86+
@AfterAll
87+
public void tearDown(SessionFactoryScope scope) {
88+
scope.inTransaction( session -> {
89+
session.createMutationQuery( "delete from EntityA" ).executeUpdate();
90+
session.createMutationQuery( "delete from EntityB" ).executeUpdate();
91+
} );
92+
}
93+
94+
@Entity( name = "EntityA" )
95+
static class EntityA {
96+
@Id
97+
private Long id;
98+
99+
@Id
100+
private String name;
101+
102+
@ManyToMany( cascade = CascadeType.ALL )
103+
@JoinTable(
104+
name = "a_b_table",
105+
joinColumns = @JoinColumn( name = "entitya_id", referencedColumnName = "id" ),
106+
inverseJoinColumns = @JoinColumn( name = "entityb_id", referencedColumnName = "id" ) )
107+
private List<EntityB> entityBList = new ArrayList<>();
108+
109+
public EntityA() {
110+
}
111+
112+
public EntityA(Long id, String name) {
113+
this.id = id;
114+
this.name = name;
115+
}
116+
117+
public List<EntityB> getEntityBList() {
118+
return entityBList;
119+
}
120+
}
121+
122+
@Entity( name = "EntityB" )
123+
static class EntityB {
124+
@Id
125+
private Long id;
126+
127+
private String name;
128+
129+
public EntityB() {
130+
}
131+
132+
public EntityB(Long id, String name) {
133+
this.id = id;
134+
this.name = name;
135+
}
136+
137+
public String getName() {
138+
return name;
139+
}
140+
}
141+
}

0 commit comments

Comments
 (0)