Skip to content

Commit a0fd643

Browse files
committed
HHH-17203 Add test for issue
1 parent 0722ea4 commit a0fd643

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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.collection;
8+
9+
import java.util.HashSet;
10+
import java.util.Set;
11+
12+
import org.hibernate.annotations.SQLRestriction;
13+
14+
import org.hibernate.testing.jdbc.SQLStatementInspector;
15+
import org.hibernate.testing.orm.junit.DomainModel;
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.CollectionTable;
23+
import jakarta.persistence.ElementCollection;
24+
import jakarta.persistence.Embeddable;
25+
import jakarta.persistence.Entity;
26+
import jakarta.persistence.Id;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* @author Marco Belladelli
32+
*/
33+
@DomainModel( annotatedClasses = {
34+
ElementCollectionSQLRestrictionTest.TaskEntity.class,
35+
ElementCollectionSQLRestrictionTest.LocalizedLabel.class,
36+
} )
37+
@SessionFactory( useCollectingStatementInspector = true )
38+
public class ElementCollectionSQLRestrictionTest {
39+
@BeforeAll
40+
public void setUp(SessionFactoryScope scope) {
41+
scope.inTransaction( session -> {
42+
final TaskEntity task1 = new TaskEntity( 1 );
43+
task1.getNames().add( new LocalizedLabel( "task:name", "test-name" ) );
44+
task1.getTypes().add( new LocalizedLabel( "task:type", "test-type" ) );
45+
session.persist( task1 );
46+
final TaskEntity task2 = new TaskEntity( 2 );
47+
task2.getNames().addAll( Set.of(
48+
new LocalizedLabel( "task:name", "test-name1" ),
49+
new LocalizedLabel( "task:name", "test-name2" )
50+
) );
51+
task2.getTypes().addAll( Set.of(
52+
new LocalizedLabel( "task:type", "test-type1" ),
53+
new LocalizedLabel( "task:type", "test-type2" )
54+
) );
55+
session.persist( task2 );
56+
} );
57+
}
58+
59+
@AfterAll
60+
public void tearDown(SessionFactoryScope scope) {
61+
scope.inTransaction( session -> session.createMutationQuery( "delete from TaskEntity" ).executeUpdate() );
62+
}
63+
64+
@Test
65+
public void testRemoveEmptyCollection(SessionFactoryScope scope) {
66+
final SQLStatementInspector inspector = scope.getCollectingStatementInspector();
67+
scope.inTransaction( session -> {
68+
final TaskEntity task = session.find( TaskEntity.class, 1 );
69+
assertThat( task.getNames() ).hasSize( 1 );
70+
assertThat( task.getTypes() ).hasSize( 1 );
71+
task.getNames().clear();
72+
inspector.clear();
73+
} );
74+
assertThat( inspector.getSqlQueries() ).hasSize( 3 );
75+
inspector.getSqlQueries().forEach( query -> assertThat( query ).containsAnyOf( "task:", "insert" ) );
76+
scope.inTransaction( session -> {
77+
final TaskEntity task = session.find( TaskEntity.class, 1 );
78+
assertThat( task.getNames() ).hasSize( 0 );
79+
assertThat( task.getTypes() ).hasSize( 1 );
80+
} );
81+
}
82+
83+
@Test
84+
public void testRemoveNonEmptyCollection(SessionFactoryScope scope) {
85+
final SQLStatementInspector inspector = scope.getCollectingStatementInspector();
86+
scope.inTransaction( session -> {
87+
final TaskEntity task = session.find( TaskEntity.class, 2 );
88+
assertThat( task.getNames() ).hasSize( 2 );
89+
assertThat( task.getTypes() ).hasSize( 2 );
90+
task.getTypes().remove( task.getTypes().iterator().next() );
91+
inspector.clear();
92+
} );
93+
assertThat( inspector.getSqlQueries() ).hasSize( 5 );
94+
inspector.getSqlQueries().forEach( query -> assertThat( query ).containsAnyOf( "task:", "insert" ) );
95+
scope.inTransaction( session -> {
96+
final TaskEntity task = session.find( TaskEntity.class, 2 );
97+
assertThat( task.getNames() ).hasSize( 2 );
98+
assertThat( task.getTypes() ).hasSize( 1 );
99+
} );
100+
}
101+
102+
@Test
103+
public void testUpdate(SessionFactoryScope scope) {
104+
final SQLStatementInspector inspector = scope.getCollectingStatementInspector();
105+
scope.inTransaction( session -> {
106+
final TaskEntity task = session.find( TaskEntity.class, 2 );
107+
task.getNames().forEach( n -> n.setLabel( n.getLabel().replace( "test", "updated" ) ) );
108+
inspector.clear();
109+
} );
110+
assertThat( inspector.getSqlQueries() ).hasSize( 3 );
111+
inspector.getSqlQueries().forEach( query -> assertThat( query ).containsAnyOf( "task:", "insert" ) );
112+
scope.inTransaction( session -> {
113+
final TaskEntity task = session.find( TaskEntity.class, 2 );
114+
task.getNames().forEach( n -> assertThat( n.getLabel() ).startsWith( "updated" ) );
115+
task.getTypes().forEach( t -> assertThat( t.getLabel() ).startsWith( "test" ) );
116+
} );
117+
}
118+
119+
@Entity( name = "TaskEntity" )
120+
public static class TaskEntity {
121+
@Id
122+
private Integer id;
123+
124+
@ElementCollection
125+
@CollectionTable( name = "t_localized_label" )
126+
@SQLRestriction( "identifier ='task:name'" )
127+
private Set<LocalizedLabel> names = new HashSet<>();
128+
129+
@ElementCollection
130+
@CollectionTable( name = "t_localized_label" )
131+
@SQLRestriction( "identifier ='task:type'" )
132+
private Set<LocalizedLabel> types = new HashSet<>();
133+
134+
public TaskEntity() {
135+
}
136+
137+
public TaskEntity(Integer id) {
138+
this.id = id;
139+
}
140+
141+
public int getId() {
142+
return id;
143+
}
144+
145+
public Set<LocalizedLabel> getNames() {
146+
return names;
147+
}
148+
149+
public Set<LocalizedLabel> getTypes() {
150+
return types;
151+
}
152+
}
153+
154+
@Embeddable
155+
public static class LocalizedLabel {
156+
private String identifier;
157+
158+
private String label;
159+
160+
public LocalizedLabel() {
161+
}
162+
163+
public LocalizedLabel(String identifier, String label) {
164+
this.identifier = identifier;
165+
this.label = label;
166+
}
167+
168+
public String getIdentifier() {
169+
return identifier;
170+
}
171+
172+
public String getLabel() {
173+
return label;
174+
}
175+
176+
public void setLabel(String label) {
177+
this.label = label;
178+
}
179+
}
180+
}

0 commit comments

Comments
 (0)