Skip to content

Commit d2acefd

Browse files
committed
HHH-3354 Test auto flush remove previously added entity from collection
1 parent 1a1bc8f commit d2acefd

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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.flush;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.FetchType;
9+
import jakarta.persistence.GeneratedValue;
10+
import jakarta.persistence.Id;
11+
import jakarta.persistence.ManyToOne;
12+
import jakarta.persistence.OneToMany;
13+
import org.hibernate.testing.orm.junit.DomainModel;
14+
import org.hibernate.testing.orm.junit.Jira;
15+
import org.hibernate.testing.orm.junit.SessionFactory;
16+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
17+
import org.junit.jupiter.api.AfterAll;
18+
import org.junit.jupiter.api.BeforeAll;
19+
import org.junit.jupiter.api.Test;
20+
21+
import java.util.HashSet;
22+
import java.util.Set;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
@SessionFactory
27+
@DomainModel( annotatedClasses = {
28+
AutoFlushRemoveTest.Item.class,
29+
AutoFlushRemoveTest.Comment.class,
30+
} )
31+
@Jira( "https://hibernate.atlassian.net/browse/HHH-3354" )
32+
public class AutoFlushRemoveTest {
33+
@BeforeAll
34+
public void setUp(SessionFactoryScope scope) {
35+
scope.inTransaction( session -> {
36+
final Item first = new Item( 1L, "item" );
37+
session.persist( first );
38+
} );
39+
}
40+
41+
@AfterAll
42+
public void tearDown(SessionFactoryScope scope) {
43+
scope.inTransaction( session -> {
44+
session.createMutationQuery( "delete from Comment" ).executeUpdate();
45+
session.createMutationQuery( "delete from Item" ).executeUpdate();
46+
} );
47+
}
48+
49+
@Test
50+
public void test(SessionFactoryScope scope) {
51+
scope.inTransaction( session -> {
52+
final Item item = session.find( Item.class, 1L );
53+
54+
final Comment comment1 = new Comment( "c1", item );
55+
session.persist( comment1 );
56+
final Comment comment2 = new Comment( "c2", item );
57+
session.persist( comment2 );
58+
item.getComments().add( comment1 );
59+
item.getComments().add( comment2 );
60+
61+
session.flush();
62+
63+
session.remove( comment1 );
64+
comment1.item = null;
65+
} );
66+
scope.inTransaction( session -> {
67+
final Item item = session.find( Item.class, 1L );
68+
assertThat( item.getComments().size() ).isEqualTo( 1 );
69+
});
70+
}
71+
72+
@Entity( name = "Item" )
73+
public static class Item {
74+
@Id
75+
private Long id;
76+
private String name;
77+
@OneToMany(mappedBy = "item")
78+
private Set<Comment> comments = new HashSet<>();
79+
80+
public Item() {
81+
}
82+
83+
public Item(Long id, String name) {
84+
this.id = id;
85+
this.name = name;
86+
}
87+
88+
public String getName() {
89+
return name;
90+
}
91+
92+
public Set<Comment> getComments() {
93+
return comments;
94+
}
95+
}
96+
97+
@Entity( name = "Comment" )
98+
public static class Comment {
99+
@Id
100+
@GeneratedValue
101+
private Long id;
102+
private String text;
103+
@ManyToOne(fetch = FetchType.LAZY)
104+
private Item item;
105+
106+
public Comment() {
107+
}
108+
109+
public Comment(String text, Item item) {
110+
this.text = text;
111+
this.item = item;
112+
}
113+
114+
public String getText() {
115+
return text;
116+
}
117+
118+
public Item getItem() {
119+
return item;
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)