|
| 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.query.criteria.JpaCriteriaInsertValues; |
| 14 | + |
| 15 | +import org.junit.jupiter.api.BeforeEach; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +import io.vertx.junit5.Timeout; |
| 19 | +import io.vertx.junit5.VertxTestContext; |
| 20 | +import jakarta.persistence.Entity; |
| 21 | +import jakarta.persistence.Id; |
| 22 | +import jakarta.persistence.Table; |
| 23 | + |
| 24 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | + |
| 27 | +@Timeout(value = 10, timeUnit = MINUTES) |
| 28 | +public class MutationQueryInsertCriteriaTest extends BaseReactiveTest { |
| 29 | + |
| 30 | + Flour spelt = new Flour( 1, "Spelt", "An ancient grain, is a hexaploid species of wheat.", "Wheat flour" ); |
| 31 | + |
| 32 | + @Override |
| 33 | + protected Collection<Class<?>> annotatedEntities() { |
| 34 | + return List.of( Flour.class ); |
| 35 | + } |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + public void populateDb(VertxTestContext context) { |
| 39 | + test( context, getSessionFactory().withTransaction( s -> s.persist( spelt ) ) ); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testStageInsertCriteriaQuery(VertxTestContext context) { |
| 44 | + final int id = 2; |
| 45 | + final String name = "Rye"; |
| 46 | + final String description = "Used to bake the traditional sourdough breads of Germany."; |
| 47 | + final String type = "Wheat flour"; |
| 48 | + test( |
| 49 | + context, |
| 50 | + openSession() |
| 51 | + .thenCompose( s -> { |
| 52 | + HibernateCriteriaBuilder cb = s.getFactory().getCriteriaBuilder(); |
| 53 | + JpaCriteriaInsertValues<Flour> insert = cb.createCriteriaInsertValues( Flour.class ); |
| 54 | + insert.setInsertionTargetPaths( |
| 55 | + insert.getTarget().get( "id" ), |
| 56 | + insert.getTarget().get( "name" ), |
| 57 | + insert.getTarget().get( "description" ), |
| 58 | + insert.getTarget().get( "type" ) |
| 59 | + ); |
| 60 | + insert.values( |
| 61 | + cb.values( |
| 62 | + cb.value( id ), |
| 63 | + cb.value( name ), |
| 64 | + cb.value( description ), |
| 65 | + cb.value( type ) |
| 66 | + ) ); |
| 67 | + return s.createMutationQuery( insert ).executeUpdate(); |
| 68 | + } ) |
| 69 | + .thenAccept( resultCount -> assertThat( resultCount ).isEqualTo( 1 ) ) |
| 70 | + .thenCompose( v -> openSession() ) |
| 71 | + .thenCompose( s -> s.find( Flour.class, id ) ) |
| 72 | + .thenAccept( result -> { |
| 73 | + assertThat( result ).isNotNull(); |
| 74 | + assertThat( result.name ).isEqualTo( name ); |
| 75 | + assertThat( result.description ).isEqualTo( description ); |
| 76 | + assertThat( result.type ).isEqualTo( type ); |
| 77 | + } ) |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testMutinyInsertCriteriaQuery(VertxTestContext context) { |
| 83 | + final int id = 3; |
| 84 | + final String name = "Almond"; |
| 85 | + final String description = "made from ground almonds."; |
| 86 | + final String type = "Gluten free"; |
| 87 | + test( |
| 88 | + context, |
| 89 | + openMutinySession() |
| 90 | + .chain( s -> { |
| 91 | + HibernateCriteriaBuilder cb = s.getFactory().getCriteriaBuilder(); |
| 92 | + JpaCriteriaInsertValues<Flour> insert = cb.createCriteriaInsertValues( Flour.class ); |
| 93 | + insert.setInsertionTargetPaths( |
| 94 | + insert.getTarget().get( "id" ), |
| 95 | + insert.getTarget().get( "name" ), |
| 96 | + insert.getTarget().get( "description" ), |
| 97 | + insert.getTarget().get( "type" ) |
| 98 | + ); |
| 99 | + insert.values( |
| 100 | + cb.values( |
| 101 | + cb.value( id ), |
| 102 | + cb.value( name ), |
| 103 | + cb.value( description ), |
| 104 | + cb.value( type ) |
| 105 | + ) ); |
| 106 | + return s.createMutationQuery( insert ).executeUpdate(); |
| 107 | + } ) |
| 108 | + .invoke( resultCount -> assertThat( resultCount ).isEqualTo( 1 ) ) |
| 109 | + .chain( v -> openMutinySession() ) |
| 110 | + .chain( s -> s.find( Flour.class, id ) ) |
| 111 | + .invoke( result -> { |
| 112 | + assertThat( result ).isNotNull(); |
| 113 | + assertThat( result.name ).isEqualTo( name ); |
| 114 | + assertThat( result.description ).isEqualTo( description ); |
| 115 | + assertThat( result.type ).isEqualTo( type ); |
| 116 | + } ) |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + @Entity(name = "Flour") |
| 121 | + @Table(name = "Flour") |
| 122 | + public static class Flour { |
| 123 | + @Id |
| 124 | + private Integer id; |
| 125 | + private String name; |
| 126 | + private String description; |
| 127 | + private String type; |
| 128 | + |
| 129 | + public Flour() { |
| 130 | + } |
| 131 | + |
| 132 | + public Flour(Integer id, String name, String description, String type) { |
| 133 | + this.id = id; |
| 134 | + this.name = name; |
| 135 | + this.description = description; |
| 136 | + this.type = type; |
| 137 | + } |
| 138 | + |
| 139 | + public Integer getId() { |
| 140 | + return id; |
| 141 | + } |
| 142 | + |
| 143 | + public void setId(Integer id) { |
| 144 | + this.id = id; |
| 145 | + } |
| 146 | + |
| 147 | + public String getName() { |
| 148 | + return name; |
| 149 | + } |
| 150 | + |
| 151 | + public void setName(String name) { |
| 152 | + this.name = name; |
| 153 | + } |
| 154 | + |
| 155 | + public String getDescription() { |
| 156 | + return description; |
| 157 | + } |
| 158 | + |
| 159 | + public void setDescription(String description) { |
| 160 | + this.description = description; |
| 161 | + } |
| 162 | + |
| 163 | + public String getType() { |
| 164 | + return type; |
| 165 | + } |
| 166 | + |
| 167 | + public void setType(String type) { |
| 168 | + this.type = type; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public String toString() { |
| 173 | + return name; |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public boolean equals(Object o) { |
| 178 | + if ( this == o ) { |
| 179 | + return true; |
| 180 | + } |
| 181 | + if ( o == null || getClass() != o.getClass() ) { |
| 182 | + return false; |
| 183 | + } |
| 184 | + Flour flour = (Flour) o; |
| 185 | + return Objects.equals( name, flour.name ) && |
| 186 | + Objects.equals( description, flour.description ) && |
| 187 | + Objects.equals( type, flour.type ); |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public int hashCode() { |
| 192 | + return Objects.hash( name, description, type ); |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments