|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.id.usertype.inet; |
| 6 | + |
| 7 | +import org.hibernate.boot.model.TypeContributions; |
| 8 | +import org.hibernate.boot.model.TypeContributor; |
| 9 | +import org.hibernate.query.NativeQuery; |
| 10 | +import org.hibernate.service.ServiceRegistry; |
| 11 | +import org.hibernate.testing.orm.junit.BootstrapServiceRegistry; |
| 12 | +import org.hibernate.testing.orm.junit.DialectFeatureChecks; |
| 13 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 14 | +import org.hibernate.testing.orm.junit.RequiresDialectFeature; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 17 | +import org.junit.jupiter.api.AfterEach; |
| 18 | +import org.junit.jupiter.api.Assertions; |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Vlad Mihalcea |
| 26 | + */ |
| 27 | +@SuppressWarnings("JUnitMalformedDeclaration") |
| 28 | +@RequiresDialectFeature(feature = DialectFeatureChecks.IsPgJdbc.class) |
| 29 | +@BootstrapServiceRegistry( javaServices = @BootstrapServiceRegistry.JavaService( |
| 30 | + role = TypeContributor.class, |
| 31 | + impl = PostgreSQLInetTypeTests.TypeContributorImpl.class |
| 32 | +) ) |
| 33 | +@DomainModel(annotatedClasses = Event.class) |
| 34 | +@SessionFactory |
| 35 | +public class PostgreSQLInetTypeTests { |
| 36 | + @BeforeEach |
| 37 | + void setUp(SessionFactoryScope factoryScope) { |
| 38 | + factoryScope.inTransaction( (session) -> { |
| 39 | + var event = new Event(); |
| 40 | + event.setId( 1L ); |
| 41 | + event.setIp( "192.168.0.123/24" ); |
| 42 | + |
| 43 | + session.persist( event ); |
| 44 | + } ); |
| 45 | + } |
| 46 | + |
| 47 | + @AfterEach |
| 48 | + void tearDown(SessionFactoryScope factoryScope) { |
| 49 | + factoryScope.dropData(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testJPQL(SessionFactoryScope factoryScope) { |
| 54 | + factoryScope.inTransaction( entityManager -> { |
| 55 | + var inets = entityManager.createQuery( |
| 56 | + "select e.ip " + |
| 57 | + "from Event e " + |
| 58 | + "where e.id = :id", Inet.class ) |
| 59 | + .setParameter( "id", 1L ) |
| 60 | + .getResultList(); |
| 61 | + |
| 62 | + Assertions.assertEquals( 1, inets.size() ); |
| 63 | + Assertions.assertEquals( "192.168.0.123/24", inets.get( 0 ).getAddress() ); |
| 64 | + } ); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testNativeSQLAddScalar(SessionFactoryScope factoryScope) { |
| 69 | + factoryScope.inTransaction( entityManager -> { |
| 70 | + List<Inet> inets = entityManager.createNativeQuery( |
| 71 | + "select e.ip as ip " + |
| 72 | + "from Event e " + |
| 73 | + "where e.id = :id" ) |
| 74 | + .setParameter( "id", 1L ) |
| 75 | + .unwrap( NativeQuery.class ) |
| 76 | + .addScalar( "ip", InetType.INSTANCE ) |
| 77 | + .getResultList(); |
| 78 | + |
| 79 | + Assertions.assertEquals( 1, inets.size() ); |
| 80 | + Assertions.assertEquals( "192.168.0.123/24", inets.get( 0 ).getAddress() ); |
| 81 | + } ); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + public static class TypeContributorImpl implements TypeContributor { |
| 86 | + @Override |
| 87 | + public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) { |
| 88 | + typeContributions.contributeJavaType( InetJavaType.INSTANCE ); |
| 89 | + typeContributions.contributeJdbcType( InetJdbcType.INSTANCE ); |
| 90 | + |
| 91 | + var typeConfiguration = typeContributions.getTypeConfiguration(); |
| 92 | + typeConfiguration.getBasicTypeRegistry().register( InetType.INSTANCE ); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments