|
| 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.types; |
| 7 | + |
| 8 | +import java.math.BigDecimal; |
| 9 | +import java.util.Collection; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Objects; |
| 12 | +import java.util.concurrent.CompletionStage; |
| 13 | + |
| 14 | +import org.hibernate.reactive.BaseReactiveTest; |
| 15 | +import org.hibernate.reactive.util.impl.CompletionStages; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import io.vertx.core.json.JsonObject; |
| 20 | +import io.vertx.junit5.Timeout; |
| 21 | +import io.vertx.junit5.VertxTestContext; |
| 22 | +import jakarta.persistence.Column; |
| 23 | +import jakarta.persistence.Entity; |
| 24 | +import jakarta.persistence.Id; |
| 25 | +import jakarta.persistence.Table; |
| 26 | +import org.assertj.core.api.Assertions; |
| 27 | + |
| 28 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 29 | + |
| 30 | +/** |
| 31 | + * Test types that we expect to work only on selected DBs. |
| 32 | + */ |
| 33 | +@Timeout(value = 10, timeUnit = MINUTES) |
| 34 | + |
| 35 | +public class JsonWithBigDecimalTypeTest extends BaseReactiveTest { |
| 36 | + |
| 37 | + private final static BigDecimal TAO = BigDecimal.valueOf( 6.2832 ); |
| 38 | + |
| 39 | + @Override |
| 40 | + protected Collection<Class<?>> annotatedEntities() { |
| 41 | + return List.of( Book.class ); |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + @Override |
| 46 | + protected CompletionStage<Void> cleanDb() { |
| 47 | + return CompletionStages.voidFuture(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testJsonType(VertxTestContext context) { |
| 52 | + JsonObject jsonPrice = new JsonObject().put( "amount", TAO ); |
| 53 | + final Book theBookOfM = new Book( 5, "The Book of M", jsonPrice ); |
| 54 | + test( context, getMutinySessionFactory() |
| 55 | + .withTransaction( session -> session.persist( theBookOfM ) ) |
| 56 | + .chain( () -> getMutinySessionFactory().withTransaction( s -> { |
| 57 | + return s.createNativeQuery( "select id,title,price from BookWithJson b where (b.price ->> 'amount')::decimal between ?1 and ?2" ) |
| 58 | + .setParameter( 1, BigDecimal.valueOf( 4.0 ) ) |
| 59 | + .setParameter( 2, BigDecimal.valueOf( 100.0 ) ) |
| 60 | + .getSingleResult(); |
| 61 | + } ) ) |
| 62 | + .invoke( result -> Assertions.assertThat( result ).isEqualTo( theBookOfM ) ) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + @Entity(name = "Book") |
| 67 | + @Table(name = "BookWithJson") |
| 68 | + public static class Book { |
| 69 | + |
| 70 | + @Id |
| 71 | + Integer id; |
| 72 | + |
| 73 | + String title; |
| 74 | + |
| 75 | + @Column(name = "price") |
| 76 | + JsonObject price; |
| 77 | + |
| 78 | + public Book() { |
| 79 | + } |
| 80 | + |
| 81 | + public Book(Integer id, String title, JsonObject price) { |
| 82 | + this.id = id; |
| 83 | + this.title = title; |
| 84 | + this.price = price; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public boolean equals(Object o) { |
| 89 | + if ( this == o ) { |
| 90 | + return true; |
| 91 | + } |
| 92 | + if ( o == null || getClass() != o.getClass() ) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + Book book = (Book) o; |
| 96 | + return Objects.equals( title, book.title ); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public int hashCode() { |
| 101 | + return Objects.hashCode( title ); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments