|
6 | 6 | package org.hibernate.reactive; |
7 | 7 |
|
8 | 8 | import java.util.ArrayList; |
| 9 | +import java.util.Collection; |
9 | 10 | import java.util.List; |
10 | 11 | import javax.persistence.CascadeType; |
11 | 12 | import javax.persistence.Entity; |
|
17 | 18 | import javax.persistence.OneToMany; |
18 | 19 | import javax.persistence.Table; |
19 | 20 |
|
| 21 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
20 | 22 | import org.hibernate.cfg.Configuration; |
21 | | -import org.hibernate.reactive.provider.Settings; |
| 23 | +import org.hibernate.reactive.testing.DatabaseSelectionRule; |
| 24 | +import org.hibernate.reactive.testing.SqlStatementTracker; |
22 | 25 |
|
23 | | -import org.junit.After; |
| 26 | +import org.junit.Rule; |
24 | 27 | import org.junit.Test; |
25 | 28 |
|
26 | 29 | import io.vertx.ext.unit.TestContext; |
27 | 30 |
|
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.POSTGRESQL; |
| 33 | + |
| 34 | +/** |
| 35 | + * Test that's not necessary to do a fetch when we want to add a new element to an association. |
| 36 | + */ |
28 | 37 | public class FetchedAssociationTest extends BaseReactiveTest { |
29 | 38 |
|
30 | | - @Override |
31 | | - protected Configuration constructConfiguration() { |
32 | | - Configuration configuration = super.constructConfiguration(); |
33 | | - configuration.addAnnotatedClass( Parent.class ); |
34 | | - configuration.addAnnotatedClass( Child.class ); |
35 | | - configuration.setProperty( Settings.SHOW_SQL, "true"); |
36 | | - return configuration; |
37 | | - } |
38 | | - |
39 | | - @After |
40 | | - public void cleanDb(TestContext context) { |
41 | | - test( context, deleteEntities( Child.class, Parent.class ) ); |
42 | | - } |
43 | | - |
44 | | - @Test |
45 | | - public void testWithMutiny(TestContext context) { |
46 | | - test( context, getMutinySessionFactory() |
47 | | - .withTransaction( s -> { |
48 | | - final Parent parent = new Parent(); |
49 | | - parent.setName( "Parent" ); |
50 | | - return s.persist( parent ); |
51 | | - } ) |
52 | | - .call( () -> getMutinySessionFactory() |
53 | | - .withTransaction( s -> s |
54 | | - .createQuery( "From Parent", Parent.class ) |
55 | | - .getSingleResult() |
56 | | - .call( parent -> { |
57 | | - Child child = new Child(); |
58 | | - child.setParent( parent ); |
59 | | - parent.getChildren().add( child ); |
60 | | - return s.persist( child ); |
61 | | - } ) |
62 | | - ) |
63 | | - ) |
64 | | - ); |
65 | | - } |
66 | | - |
67 | | - @Entity(name = "Parent") |
68 | | - @Table(name = "PARENT") |
69 | | - public static class Parent { |
70 | | - @Id |
71 | | - @GeneratedValue |
72 | | - private Long id; |
73 | | - |
74 | | - private String name; |
75 | | - |
76 | | - @OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY, mappedBy = "parent") |
77 | | - private List<Child> children = new ArrayList<>(); |
78 | | - |
79 | | - public Long getId() { |
80 | | - return id; |
81 | | - } |
82 | | - |
83 | | - public void setId(Long id) { |
84 | | - this.id = id; |
85 | | - } |
86 | | - |
87 | | - public String getName() { |
88 | | - return name; |
89 | | - } |
90 | | - |
91 | | - public void setName(String name) { |
92 | | - this.name = name; |
93 | | - } |
94 | | - |
95 | | - public List<Child> getChildren() { |
96 | | - return children; |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - @Entity(name = "Child") |
101 | | - @Table(name = "CHILD") |
102 | | - public static class Child { |
103 | | - @Id |
104 | | - @GeneratedValue |
105 | | - private Long id; |
106 | | - |
107 | | - private String name; |
108 | | - |
109 | | - @ManyToOne |
110 | | - @JoinColumn(name = "lazy_parent_id") |
111 | | - private Parent parent; |
112 | | - |
113 | | - public Long getId() { |
114 | | - return id; |
115 | | - } |
116 | | - |
117 | | - public void setId(Long id) { |
118 | | - this.id = id; |
119 | | - } |
120 | | - |
121 | | - public Parent getParent() { |
122 | | - return parent; |
123 | | - } |
124 | | - |
125 | | - public void setParent(Parent parent) { |
126 | | - this.parent = parent; |
127 | | - } |
128 | | - |
129 | | - public String getName() { |
130 | | - return name; |
131 | | - } |
132 | | - |
133 | | - public void setName(String name) { |
134 | | - this.name = name; |
135 | | - } |
136 | | - } |
| 39 | + @Rule // We use native queries, they might be different for other DBs |
| 40 | + public DatabaseSelectionRule rule = DatabaseSelectionRule.runOnlyFor( POSTGRESQL ); |
| 41 | + |
| 42 | + private SqlStatementTracker sqlTracker; |
| 43 | + |
| 44 | + @Override |
| 45 | + protected Collection<Class<?>> annotatedEntities() { |
| 46 | + return List.of( Child.class, Parent.class ); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected Configuration constructConfiguration() { |
| 51 | + Configuration configuration = super.constructConfiguration(); |
| 52 | + sqlTracker = new SqlStatementTracker( FetchedAssociationTest::isSelectOrInsertQuery, configuration.getProperties() ); |
| 53 | + return configuration; |
| 54 | + } |
| 55 | + |
| 56 | + private static boolean isSelectOrInsertQuery(String s) { |
| 57 | + return s.toLowerCase().startsWith( "select" ) |
| 58 | + || s.toLowerCase().startsWith( "insert" ); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected void addServices(StandardServiceRegistryBuilder builder) { |
| 63 | + sqlTracker.registerService( builder ); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testWithMutiny(TestContext context) { |
| 68 | + test( context, getMutinySessionFactory() |
| 69 | + .withTransaction( s -> { |
| 70 | + final Parent parent = new Parent(); |
| 71 | + parent.setName( "Parent" ); |
| 72 | + return s.persist( parent ); |
| 73 | + } ) |
| 74 | + .call( () -> getMutinySessionFactory() |
| 75 | + .withTransaction( s -> s |
| 76 | + .createQuery( "From Parent", Parent.class ) |
| 77 | + .getSingleResult() |
| 78 | + .call( parent -> { |
| 79 | + Child child = new Child(); |
| 80 | + child.setParent( parent ); |
| 81 | + // Because we are only adding a new element, we don't need to fetch the collection |
| 82 | + parent.getChildren().add( child ); |
| 83 | + return s.persist( child ); |
| 84 | + } ) |
| 85 | + ) |
| 86 | + ) |
| 87 | + .invoke( () -> assertThat( sqlTracker.getLoggedQueries() ) |
| 88 | + .containsExactly( getExpectedNativeQueries() ) ) |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + // We don't expect a select from CHILD |
| 93 | + private String[] getExpectedNativeQueries() { |
| 94 | + return new String[] { |
| 95 | + "select nextval ('hibernate_sequence')", |
| 96 | + "insert into PARENT (name, id) values ($1, $2)", |
| 97 | + "select fetchedass0_.id as id1_1_, fetchedass0_.name as name2_1_ from PARENT fetchedass0_", |
| 98 | + "select nextval ('hibernate_sequence')", |
| 99 | + "insert into CHILD (name, lazy_parent_id, id) values ($1, $2, $3)" |
| 100 | + }; |
| 101 | + } |
| 102 | + |
| 103 | + @Entity(name = "Parent") |
| 104 | + @Table(name = "PARENT") |
| 105 | + public static class Parent { |
| 106 | + @Id |
| 107 | + @GeneratedValue |
| 108 | + private Long id; |
| 109 | + |
| 110 | + private String name; |
| 111 | + |
| 112 | + @OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY, mappedBy = "parent") |
| 113 | + private List<Child> children = new ArrayList<>(); |
| 114 | + |
| 115 | + public Long getId() { |
| 116 | + return id; |
| 117 | + } |
| 118 | + |
| 119 | + public void setId(Long id) { |
| 120 | + this.id = id; |
| 121 | + } |
| 122 | + |
| 123 | + public String getName() { |
| 124 | + return name; |
| 125 | + } |
| 126 | + |
| 127 | + public void setName(String name) { |
| 128 | + this.name = name; |
| 129 | + } |
| 130 | + |
| 131 | + public List<Child> getChildren() { |
| 132 | + return children; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + @Entity(name = "Child") |
| 137 | + @Table(name = "CHILD") |
| 138 | + public static class Child { |
| 139 | + @Id |
| 140 | + @GeneratedValue |
| 141 | + private Long id; |
| 142 | + |
| 143 | + private String name; |
| 144 | + |
| 145 | + @ManyToOne |
| 146 | + @JoinColumn(name = "lazy_parent_id") |
| 147 | + private Parent parent; |
| 148 | + |
| 149 | + public Long getId() { |
| 150 | + return id; |
| 151 | + } |
| 152 | + |
| 153 | + public void setId(Long id) { |
| 154 | + this.id = id; |
| 155 | + } |
| 156 | + |
| 157 | + public Parent getParent() { |
| 158 | + return parent; |
| 159 | + } |
| 160 | + |
| 161 | + public void setParent(Parent parent) { |
| 162 | + this.parent = parent; |
| 163 | + } |
| 164 | + |
| 165 | + public String getName() { |
| 166 | + return name; |
| 167 | + } |
| 168 | + |
| 169 | + public void setName(String name) { |
| 170 | + this.name = name; |
| 171 | + } |
| 172 | + } |
137 | 173 | } |
0 commit comments