|
| 1 | +/* Hibernate, Relational Persistence for Idiomatic Java |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 5 | + */ |
| 6 | +package org.hibernate.reactive; |
| 7 | + |
| 8 | +import java.util.Collection; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Objects; |
| 11 | + |
| 12 | +import org.hibernate.query.criteria.HibernateCriteriaBuilder; |
| 13 | +import org.hibernate.reactive.mutiny.Mutiny; |
| 14 | +import org.hibernate.reactive.stage.Stage; |
| 15 | + |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import io.vertx.junit5.Timeout; |
| 20 | +import io.vertx.junit5.VertxTestContext; |
| 21 | +import jakarta.persistence.Entity; |
| 22 | +import jakarta.persistence.Id; |
| 23 | +import jakarta.persistence.Table; |
| 24 | +import jakarta.persistence.criteria.CriteriaBuilder; |
| 25 | +import jakarta.persistence.criteria.CriteriaDelete; |
| 26 | +import jakarta.persistence.criteria.CriteriaUpdate; |
| 27 | +import jakarta.persistence.criteria.Root; |
| 28 | + |
| 29 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | + |
| 32 | +@Timeout(value = 10, timeUnit = MINUTES) |
| 33 | +public class CriteriaMutationQueryTest extends BaseReactiveTest { |
| 34 | + Flour spelt = new Flour( 1, "Spelt", "An ancient grain, is a hexaploid species of wheat.", "Wheat flour" ); |
| 35 | + Flour rye = new Flour( 2, "Rye", "Used to bake the traditional sourdough breads of Germany.", "Wheat flour" ); |
| 36 | + Flour almond = new Flour( 3, "Almond", "made from ground almonds.", "Gluten free" ); |
| 37 | + |
| 38 | + @Override |
| 39 | + protected Collection<Class<?>> annotatedEntities() { |
| 40 | + return List.of( Flour.class ); |
| 41 | + } |
| 42 | + |
| 43 | + @BeforeEach |
| 44 | + public void populateDb(VertxTestContext context) { |
| 45 | + test( context, getSessionFactory().withTransaction( s -> s.persist( spelt, rye, almond ) ) ); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testStageUpdateCriteriaQuery(VertxTestContext context) { |
| 50 | + String updatedDescription = "Most rye breads use a mix of rye and wheat flours"; |
| 51 | + test( context, getSessionFactory() |
| 52 | + .withTransaction( s -> s |
| 53 | + .createMutationQuery( criteriaUpdate( getCriteriaBuilder( s ), updatedDescription, rye ) ) |
| 54 | + .executeUpdate() |
| 55 | + ) |
| 56 | + .thenAccept( resultCount -> assertThat( resultCount ).isEqualTo( 1 ) ) |
| 57 | + .thenCompose( v -> getSessionFactory() |
| 58 | + .withTransaction( s -> s.find( Flour.class, rye.getId() ) ) ) |
| 59 | + .thenAccept( result -> assertThat( result.getDescription() ).isEqualTo( updatedDescription ) ) |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testMutinyUpdateCriteriaQuery(VertxTestContext context) { |
| 65 | + String updatedDescription = "made from ground almonds."; |
| 66 | + test( context, getMutinySessionFactory() |
| 67 | + .withTransaction( s -> s |
| 68 | + .createMutationQuery( criteriaUpdate( getCriteriaBuilder( s ), updatedDescription, almond ) ) |
| 69 | + .executeUpdate() |
| 70 | + ) |
| 71 | + .invoke( resultCount -> assertThat( resultCount ).isEqualTo( 1 ) ) |
| 72 | + .chain( v -> getMutinySessionFactory() |
| 73 | + .withTransaction( s -> s.find( Flour.class, almond.getId() ) ) ) |
| 74 | + .invoke( result -> assertThat( result.getDescription() ).isEqualTo( updatedDescription ) ) |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testStageDeleteCriteriaQuery(VertxTestContext context) { |
| 80 | + test( context, getSessionFactory() |
| 81 | + .withTransaction( s -> s |
| 82 | + .createMutationQuery( criteriaUpdate( getCriteriaBuilder( s ) ) ) |
| 83 | + .executeUpdate() |
| 84 | + ) |
| 85 | + .thenAccept( resultCount -> assertThat( resultCount ).isEqualTo( 1 ) ) |
| 86 | + .thenCompose( v -> getSessionFactory() |
| 87 | + .withTransaction( s -> s.find( Flour.class, spelt.getId() ) ) ) |
| 88 | + .thenAccept( result -> assertThat( result ).isNull() ) |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testMutinyDeleteCriteriaQuery(VertxTestContext context) { |
| 94 | + test( context, getMutinySessionFactory() |
| 95 | + .withTransaction( s -> { |
| 96 | + return s.createMutationQuery( criteriaUpdate( getCriteriaBuilder( s ) ) ).executeUpdate(); |
| 97 | + } ) |
| 98 | + .invoke( resultCount -> assertThat( resultCount ).isEqualTo( 1 ) ) |
| 99 | + .chain( v -> getMutinySessionFactory() |
| 100 | + .withTransaction( s -> s.find( Flour.class, spelt.getId() ) ) ) |
| 101 | + .invoke( result -> assertThat( result ).isNull() ) |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + private CriteriaUpdate<Flour> criteriaUpdate(CriteriaBuilder cb, String updatedDescription, Flour rye) { |
| 106 | + CriteriaUpdate<Flour> criteriaUpdate = cb.createCriteriaUpdate( Flour.class ); |
| 107 | + Root<Flour> from = criteriaUpdate.from( Flour.class ); |
| 108 | + criteriaUpdate.set( from.get( "description" ), updatedDescription ); |
| 109 | + criteriaUpdate.where( cb.equal( from.get( "id" ), rye.getId() ) ); |
| 110 | + return criteriaUpdate; |
| 111 | + } |
| 112 | + |
| 113 | + private CriteriaDelete<Flour> criteriaUpdate(CriteriaBuilder criteriaBuilder) { |
| 114 | + CriteriaDelete<Flour> criteriaDelete = criteriaBuilder.createCriteriaDelete( Flour.class ); |
| 115 | + Root<Flour> from = criteriaDelete.from( Flour.class ); |
| 116 | + criteriaDelete.where( criteriaBuilder.equal( from.get( "id" ), spelt.getId() ) ); |
| 117 | + return criteriaDelete; |
| 118 | + } |
| 119 | + |
| 120 | + private static HibernateCriteriaBuilder getCriteriaBuilder(Stage.Session session) { |
| 121 | + return session.getFactory().getCriteriaBuilder(); |
| 122 | + } |
| 123 | + |
| 124 | + private static HibernateCriteriaBuilder getCriteriaBuilder(Mutiny.Session session) { |
| 125 | + return session.getFactory().getCriteriaBuilder(); |
| 126 | + } |
| 127 | + |
| 128 | + @Entity(name = "Flour") |
| 129 | + @Table(name = "Flour") |
| 130 | + public static class Flour { |
| 131 | + @Id |
| 132 | + private Integer id; |
| 133 | + private String name; |
| 134 | + private String description; |
| 135 | + private String type; |
| 136 | + |
| 137 | + public Flour() { |
| 138 | + } |
| 139 | + |
| 140 | + public Flour(Integer id, String name, String description, String type) { |
| 141 | + this.id = id; |
| 142 | + this.name = name; |
| 143 | + this.description = description; |
| 144 | + this.type = type; |
| 145 | + } |
| 146 | + |
| 147 | + public Integer getId() { |
| 148 | + return id; |
| 149 | + } |
| 150 | + |
| 151 | + public void setId(Integer id) { |
| 152 | + this.id = id; |
| 153 | + } |
| 154 | + |
| 155 | + public String getName() { |
| 156 | + return name; |
| 157 | + } |
| 158 | + |
| 159 | + public void setName(String name) { |
| 160 | + this.name = name; |
| 161 | + } |
| 162 | + |
| 163 | + public String getDescription() { |
| 164 | + return description; |
| 165 | + } |
| 166 | + |
| 167 | + public void setDescription(String description) { |
| 168 | + this.description = description; |
| 169 | + } |
| 170 | + |
| 171 | + public String getType() { |
| 172 | + return type; |
| 173 | + } |
| 174 | + |
| 175 | + public void setType(String type) { |
| 176 | + this.type = type; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public String toString() { |
| 181 | + return name; |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public boolean equals(Object o) { |
| 186 | + if ( this == o ) { |
| 187 | + return true; |
| 188 | + } |
| 189 | + if ( o == null || getClass() != o.getClass() ) { |
| 190 | + return false; |
| 191 | + } |
| 192 | + Flour flour = (Flour) o; |
| 193 | + return Objects.equals( name, flour.name ) && |
| 194 | + Objects.equals( description, flour.description ) && |
| 195 | + Objects.equals( type, flour.type ); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public int hashCode() { |
| 200 | + return Objects.hash( name, description, type ); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments