Skip to content

Commit 4ed753e

Browse files
committed
HHH-19322 some tests
1 parent 6d30d10 commit 4ed753e

File tree

4 files changed

+291
-0
lines changed

4 files changed

+291
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.collection.stale;
6+
7+
import jakarta.persistence.ElementCollection;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.GeneratedValue;
10+
import jakarta.persistence.Id;
11+
import jakarta.persistence.OrderColumn;
12+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
13+
import org.hibernate.testing.orm.junit.Jpa;
14+
import org.hibernate.testing.orm.junit.Setting;
15+
import org.junit.jupiter.api.Test;
16+
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
20+
import static org.hibernate.cfg.BatchSettings.STATEMENT_BATCH_SIZE;
21+
22+
@Jpa(annotatedClasses = StaleListTest.StaleListTestEntity.class,
23+
properties = @Setting(name = STATEMENT_BATCH_SIZE, value = "5"))
24+
public class StaleListTest {
25+
@Test void test1(EntityManagerFactoryScope scope) {
26+
var entity = new StaleListTestEntity();
27+
entity.stringList.add( "hello" );
28+
entity.stringList.add( "world" );
29+
scope.inTransaction( session -> {
30+
session.persist( entity );
31+
} );
32+
scope.inTransaction( session -> {
33+
var e = session.find( StaleListTestEntity.class, entity.id );
34+
e.stringList.set( 0, "goodbye" );
35+
scope.inTransaction( session2 -> {
36+
var e2 = session2.find( StaleListTestEntity.class, entity.id );
37+
e2.stringList.set( 0, "BYE" );
38+
} );
39+
} );
40+
}
41+
@Test void test2(EntityManagerFactoryScope scope) {
42+
var entity = new StaleListTestEntity();
43+
entity.stringList.add( "hello" );
44+
entity.stringList.add( "world" );
45+
scope.inTransaction( session -> {
46+
session.persist( entity );
47+
} );
48+
scope.inTransaction( session -> {
49+
var e = session.find( StaleListTestEntity.class, entity.id );
50+
e.stringList.set( 1, "everyone" );
51+
scope.inTransaction( session2 -> {
52+
var e2 = session2.find( StaleListTestEntity.class, entity.id );
53+
e2.stringList.remove( 1 );
54+
} );
55+
} );
56+
}
57+
static @Entity class StaleListTestEntity {
58+
@GeneratedValue @Id
59+
long id;
60+
@ElementCollection @OrderColumn
61+
List<String> stringList = new ArrayList<>();
62+
}
63+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.collection.stale;
6+
7+
import jakarta.persistence.ElementCollection;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.GeneratedValue;
10+
import jakarta.persistence.Id;
11+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
12+
import org.hibernate.testing.orm.junit.Jpa;
13+
import org.hibernate.testing.orm.junit.Setting;
14+
import org.junit.jupiter.api.Test;
15+
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
19+
import static org.hibernate.cfg.BatchSettings.STATEMENT_BATCH_SIZE;
20+
21+
@Jpa(annotatedClasses = StaleMapTest.StaleMapTestEntity.class,
22+
properties = @Setting(name = STATEMENT_BATCH_SIZE, value = "5"))
23+
public class StaleMapTest {
24+
@Test void test1(EntityManagerFactoryScope scope) {
25+
var entity = new StaleMapTestEntity();
26+
entity.intStringMap.put( 0, "hello" );
27+
entity.intStringMap.put( 1, "world" );
28+
scope.inTransaction( session -> {
29+
session.persist( entity );
30+
} );
31+
scope.inTransaction( session -> {
32+
var e = session.find( StaleMapTestEntity.class, entity.id );
33+
e.intStringMap.put( 0, "goodbye" );
34+
scope.inTransaction( session2 -> {
35+
var e2 = session2.find( StaleMapTestEntity.class, entity.id );
36+
e2.intStringMap.put( 0, "BYE" );
37+
} );
38+
} );
39+
}
40+
@Test void test2(EntityManagerFactoryScope scope) {
41+
var entity = new StaleMapTestEntity();
42+
entity.intStringMap.put( 0, "hello" );
43+
entity.intStringMap.put( 1, "world" );
44+
scope.inTransaction( session -> {
45+
session.persist( entity );
46+
} );
47+
scope.inTransaction( session -> {
48+
var e = session.find( StaleMapTestEntity.class, entity.id );
49+
e.intStringMap.put( 1, "everyone" );
50+
scope.inTransaction( session2 -> {
51+
var e2 = session2.find( StaleMapTestEntity.class, entity.id );
52+
e2.intStringMap.remove( 1 );
53+
} );
54+
} );
55+
}
56+
static @Entity class StaleMapTestEntity {
57+
@GeneratedValue @Id
58+
long id;
59+
@ElementCollection
60+
Map<Integer,String> intStringMap = new HashMap<>();
61+
}
62+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.collection.stale;
6+
7+
import jakarta.persistence.CascadeType;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.GeneratedValue;
10+
import jakarta.persistence.Id;
11+
import jakarta.persistence.OneToMany;
12+
import jakarta.persistence.OrderColumn;
13+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
14+
import org.hibernate.testing.orm.junit.FailureExpected;
15+
import org.hibernate.testing.orm.junit.Jpa;
16+
import org.hibernate.testing.orm.junit.Setting;
17+
import org.junit.jupiter.api.Test;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import static org.hibernate.cfg.BatchSettings.STATEMENT_BATCH_SIZE;
23+
24+
@Jpa(annotatedClasses = {StaleOneToManyListTest.StaleListTestParent.class,
25+
StaleOneToManyListTest.StaleListTestChild.class},
26+
properties = @Setting(name = STATEMENT_BATCH_SIZE, value = "5"))
27+
public class StaleOneToManyListTest {
28+
@Test void test1(EntityManagerFactoryScope scope) {
29+
var entity = new StaleListTestParent();
30+
entity.childList.add( new StaleListTestChild("hello") );
31+
entity.childList.add( new StaleListTestChild("world") );
32+
scope.inTransaction( session -> {
33+
session.persist( entity );
34+
} );
35+
scope.inTransaction( session -> {
36+
var e = session.find( StaleListTestParent.class, entity.id );
37+
e.childList.set( 0, new StaleListTestChild("goodbye") );
38+
scope.inTransaction( session2 -> {
39+
var e2 = session2.find( StaleListTestParent.class, entity.id );
40+
e2.childList.set( 0, new StaleListTestChild("BYE") );
41+
} );
42+
} );
43+
}
44+
@Test void test2(EntityManagerFactoryScope scope) {
45+
var entity = new StaleListTestParent();
46+
entity.childList.add( new StaleListTestChild("hello") );
47+
entity.childList.add( new StaleListTestChild("world") );
48+
scope.inTransaction( session -> {
49+
session.persist( entity );
50+
} );
51+
scope.inTransaction( session -> {
52+
var e = session.find( StaleListTestParent.class, entity.id );
53+
e.childList.set( 1, new StaleListTestChild("everyone") );
54+
scope.inTransaction( session2 -> {
55+
var e2 = session2.find( StaleListTestParent.class, entity.id );
56+
e2.childList.remove( 1 );
57+
} );
58+
} );
59+
}
60+
@FailureExpected(reason = "ConstraintViolationException")
61+
@Test void test3(EntityManagerFactoryScope scope) {
62+
var parent1 = new StaleListTestParent();
63+
var parent2 = new StaleListTestParent();
64+
var child1 = new StaleListTestChild( "hello" );
65+
var child2 = new StaleListTestChild( "world" );
66+
parent1.childList.add( child1 );
67+
parent1.childList.add( child2 );
68+
scope.inTransaction( session -> {
69+
session.persist( parent1 );
70+
session.persist( parent2 );
71+
} );
72+
scope.inTransaction( session1 -> {
73+
var p = session1.find( StaleListTestParent.class, parent1.id );
74+
var c = session1.find( StaleListTestChild.class, child1.id );
75+
p.childList.remove( c );
76+
scope.inTransaction( session2 -> {
77+
var pp = session2.find( StaleListTestParent.class, parent2.id );
78+
var cc = session2.find( StaleListTestChild.class, child1.id );
79+
pp.childList.add( cc );
80+
} );
81+
} );
82+
}
83+
@Test void test4(EntityManagerFactoryScope scope) {
84+
var parent1 = new StaleListTestParent();
85+
var parent2 = new StaleListTestParent();
86+
var child1 = new StaleListTestChild( "hello" );
87+
var child2 = new StaleListTestChild( "world" );
88+
parent1.childList.add( child1 );
89+
parent1.childList.add( child2 );
90+
scope.inTransaction( session -> {
91+
session.persist( parent1 );
92+
session.persist( parent2 );
93+
} );
94+
scope.inTransaction( session1 -> {
95+
var p = session1.find( StaleListTestParent.class, parent1.id );
96+
var c = session1.find( StaleListTestChild.class, child1.id );
97+
p.childList.remove( c );
98+
} );
99+
scope.inTransaction( session2 -> {
100+
var pp = session2.find( StaleListTestParent.class, parent2.id );
101+
var cc = session2.find( StaleListTestChild.class, child1.id );
102+
pp.childList.add( cc );
103+
} );
104+
}
105+
static @Entity(name = "StaleParent") class StaleListTestParent {
106+
@GeneratedValue @Id
107+
long id;
108+
@OneToMany(cascade = CascadeType.PERSIST) @OrderColumn
109+
List<StaleListTestChild> childList = new ArrayList<>();
110+
}
111+
static @Entity(name = "StaleChild") class StaleListTestChild {
112+
@GeneratedValue @Id
113+
long id;
114+
String text;
115+
StaleListTestChild(String text) {
116+
this.text = text;
117+
}
118+
StaleListTestChild() {
119+
}
120+
}
121+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.collection.stale;
6+
7+
import jakarta.persistence.ElementCollection;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.Id;
10+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
11+
import org.hibernate.testing.orm.junit.Jpa;
12+
import org.hibernate.testing.orm.junit.Setting;
13+
import org.junit.jupiter.api.Test;
14+
15+
import java.util.HashSet;
16+
import java.util.Set;
17+
18+
import static org.hibernate.cfg.BatchSettings.STATEMENT_BATCH_SIZE;
19+
20+
@Jpa(annotatedClasses = StaleSetTest.StaleSetTestEntity.class,
21+
properties = @Setting(name = STATEMENT_BATCH_SIZE, value = "5"))
22+
public class StaleSetTest {
23+
@Test void test(EntityManagerFactoryScope scope) {
24+
var entity = new StaleSetTestEntity();
25+
entity.stringSet.add( "hello" );
26+
entity.stringSet.add( "world" );
27+
scope.inTransaction( session -> {
28+
session.persist( entity );
29+
} );
30+
scope.inTransaction( session -> {
31+
var e = session.find( StaleSetTestEntity.class, entity.id );
32+
e.stringSet.remove( "world" );
33+
scope.inTransaction( session2 -> {
34+
var e2 = session2.find( StaleSetTestEntity.class, entity.id );
35+
e2.stringSet.remove( "world" );
36+
} );
37+
} );
38+
}
39+
static @Entity class StaleSetTestEntity {
40+
@Id
41+
long id;
42+
@ElementCollection
43+
Set<String> stringSet = new HashSet<>();
44+
}
45+
}

0 commit comments

Comments
 (0)