|
| 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 | + |
| 13 | +import org.hibernate.reactive.BaseReactiveTest; |
| 14 | +import org.hibernate.reactive.annotations.EnabledFor; |
| 15 | + |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 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 jakarta.persistence.criteria.CriteriaBuilder; |
| 27 | +import jakarta.persistence.criteria.CriteriaQuery; |
| 28 | +import jakarta.persistence.criteria.Root; |
| 29 | + |
| 30 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.COCKROACHDB; |
| 33 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.POSTGRESQL; |
| 34 | + |
| 35 | +/** |
| 36 | + * Test types that we expect to work only on selected DBs. |
| 37 | + */ |
| 38 | +@Timeout(value = 10, timeUnit = MINUTES) |
| 39 | +@EnabledFor(POSTGRESQL) |
| 40 | +@EnabledFor(COCKROACHDB) |
| 41 | +public class JsonQueryTest extends BaseReactiveTest { |
| 42 | + |
| 43 | + private final static BigDecimal PIE = BigDecimal.valueOf( 3.1416 ); |
| 44 | + private final static BigDecimal TAO = BigDecimal.valueOf( 6.2832 ); |
| 45 | + |
| 46 | + final Book fakeHistory = new Book( 3, "Fake History", new JsonObject().put( "amount", PIE ) ); |
| 47 | + final Book theBookOfM = new Book( 5, "The Book of M", new JsonObject().put( "amount", TAO ) ); |
| 48 | + |
| 49 | + @Override |
| 50 | + protected Collection<Class<?>> annotatedEntities() { |
| 51 | + return List.of( Book.class ); |
| 52 | + } |
| 53 | + |
| 54 | + @BeforeEach |
| 55 | + public void populateDb(VertxTestContext context) { |
| 56 | + test( context, getMutinySessionFactory().withTransaction( s -> s.persistAll( theBookOfM, fakeHistory ) ) ); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void nativeQuery(VertxTestContext context) { |
| 61 | + test( context, getMutinySessionFactory() |
| 62 | + .withTransaction( s -> s |
| 63 | + .createNativeQuery( "select id,title,price from BookWithJson b where (b.price ->> 'amount')::decimal between ?1 and ?2", Book.class ) |
| 64 | + .setParameter( 1, BigDecimal.valueOf( 4.0 ) ) |
| 65 | + .setParameter( 2, BigDecimal.valueOf( 100.0 ) ) |
| 66 | + .getSingleResult() |
| 67 | + ) |
| 68 | + .invoke( result -> assertThat( result ).isEqualTo( theBookOfM ) ) |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void hqlQuery(VertxTestContext context) { |
| 74 | + test( context, getMutinySessionFactory() |
| 75 | + .withTransaction( s -> s |
| 76 | + .createSelectionQuery( "from Book where sql('(price ->> ?)::decimal', 'amount') between ?1 and ?2", Book.class ) |
| 77 | + .setParameter( 1, BigDecimal.valueOf( 4.0 ) ) |
| 78 | + .setParameter( 2, BigDecimal.valueOf( 100.0 ) ) |
| 79 | + .getSingleResult() |
| 80 | + ) |
| 81 | + .invoke( result -> assertThat( result ).isEqualTo( theBookOfM ) ) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + @Entity(name = "Book") |
| 86 | + @Table(name = "BookWithJson") |
| 87 | + public static class Book { |
| 88 | + |
| 89 | + @Id |
| 90 | + Integer id; |
| 91 | + |
| 92 | + String title; |
| 93 | + |
| 94 | + @Column(name = "price") |
| 95 | + JsonObject price; |
| 96 | + |
| 97 | + public Book() { |
| 98 | + } |
| 99 | + |
| 100 | + public Book(Integer id, String title, JsonObject price) { |
| 101 | + this.id = id; |
| 102 | + this.title = title; |
| 103 | + this.price = price; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public boolean equals(Object o) { |
| 108 | + if ( this == o ) { |
| 109 | + return true; |
| 110 | + } |
| 111 | + if ( o == null || getClass() != o.getClass() ) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + Book book = (Book) o; |
| 115 | + return Objects.equals( id, book.id ) && Objects.equals( |
| 116 | + title, |
| 117 | + book.title |
| 118 | + ) && Objects.equals( price, book.price ); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public int hashCode() { |
| 123 | + return Objects.hash( id, title, price ); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public String toString() { |
| 128 | + return id + ":" + title + ":" + price; |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments