|  | 
|  | 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.ArrayList; | 
|  | 9 | +import java.util.List; | 
|  | 10 | +import java.util.concurrent.CompletionStage; | 
|  | 11 | +import javax.persistence.Column; | 
|  | 12 | +import javax.persistence.Entity; | 
|  | 13 | +import javax.persistence.GeneratedValue; | 
|  | 14 | +import javax.persistence.GenerationType; | 
|  | 15 | +import javax.persistence.Id; | 
|  | 16 | +import javax.persistence.Table; | 
|  | 17 | + | 
|  | 18 | +import org.hibernate.annotations.ColumnTransformer; | 
|  | 19 | +import org.hibernate.cfg.AvailableSettings; | 
|  | 20 | +import org.hibernate.cfg.Configuration; | 
|  | 21 | + | 
|  | 22 | +import org.junit.Test; | 
|  | 23 | + | 
|  | 24 | +import io.vertx.ext.unit.TestContext; | 
|  | 25 | + | 
|  | 26 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2; | 
|  | 27 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType; | 
|  | 28 | + | 
|  | 29 | +/** | 
|  | 30 | + * Similar to {@link IdentityGeneratorTest} but enables SQL comments and uses a {@link ColumnTransformer}. | 
|  | 31 | + */ | 
|  | 32 | +public class IdentityGeneratorWithColumnTransformerTest extends BaseReactiveTest { | 
|  | 33 | + | 
|  | 34 | +	// Comments don't work with Db2 | 
|  | 35 | +	private static final String ENABLE_COMMENTS = String.valueOf( dbType() != DB2 ); | 
|  | 36 | + | 
|  | 37 | +	/** | 
|  | 38 | +	 * When {@link AvailableSettings#USE_GET_GENERATED_KEYS} is enabled, different | 
|  | 39 | +	 * queries will be used for each datastore to get the id | 
|  | 40 | +	 */ | 
|  | 41 | +	public static class EnableUseGetGeneratedKeys extends IdentityGeneratorWithColumnTransformerTest { | 
|  | 42 | + | 
|  | 43 | +		@Override | 
|  | 44 | +		protected Configuration constructConfiguration() { | 
|  | 45 | +			Configuration configuration = super.constructConfiguration(); | 
|  | 46 | +			configuration.setProperty( AvailableSettings.USE_GET_GENERATED_KEYS, "true" ); | 
|  | 47 | +			return configuration; | 
|  | 48 | +		} | 
|  | 49 | +	} | 
|  | 50 | + | 
|  | 51 | +	// The number of entities we want to create | 
|  | 52 | +	private static final int ENTITY_NUMBER = 100; | 
|  | 53 | + | 
|  | 54 | +	@Override | 
|  | 55 | +	protected Configuration constructConfiguration() { | 
|  | 56 | +		Configuration configuration = super.constructConfiguration(); | 
|  | 57 | +		configuration.addAnnotatedClass( EntityWithIdentity.class ); | 
|  | 58 | +		configuration.setProperty( AvailableSettings.USE_SQL_COMMENTS, ENABLE_COMMENTS ); | 
|  | 59 | +		return configuration; | 
|  | 60 | +	} | 
|  | 61 | + | 
|  | 62 | +	private CompletionStage<?> populateDb(TestContext context) { | 
|  | 63 | +		final List<EntityWithIdentity> identities = new ArrayList<>( ENTITY_NUMBER ); | 
|  | 64 | +		for ( int i = 0; i < ENTITY_NUMBER; i++ ) { | 
|  | 65 | +			identities.add( new EntityWithIdentity( i ) ); | 
|  | 66 | +		} | 
|  | 67 | +		return getSessionFactory() | 
|  | 68 | +				.withTransaction( (session, tx) -> session.persist( identities.toArray() ) ) | 
|  | 69 | +				.thenAccept( ignore -> { | 
|  | 70 | +					Long assignedId = 0L; | 
|  | 71 | +					for ( EntityWithIdentity identity : identities ) { | 
|  | 72 | +						context.assertNotNull( identity.id ); | 
|  | 73 | +						context.assertTrue( identity.id > assignedId ); | 
|  | 74 | +						assignedId = identity.id; | 
|  | 75 | +					} | 
|  | 76 | +				} ); | 
|  | 77 | +	} | 
|  | 78 | + | 
|  | 79 | +	@Test | 
|  | 80 | +	public void testIdentityGenerator(TestContext context) { | 
|  | 81 | +		test( context, populateDb( context ) | 
|  | 82 | +				.thenCompose( v -> openSession() ) | 
|  | 83 | +				.thenCompose( session -> | 
|  | 84 | +					  session.createQuery( "FROM EntityWithIdentity ORDER BY position ASC", EntityWithIdentity.class ) | 
|  | 85 | +					  .getResultList() ) | 
|  | 86 | +				.thenAccept( list -> { | 
|  | 87 | +					context.assertEquals( ENTITY_NUMBER, list.size() ); | 
|  | 88 | +					int i = 0; | 
|  | 89 | +					for ( EntityWithIdentity entity : list ) { | 
|  | 90 | +						context.assertEquals( i * 2, entity.getPosition() ); | 
|  | 91 | +						i++; | 
|  | 92 | +					} | 
|  | 93 | +				} ) ); | 
|  | 94 | +	} | 
|  | 95 | + | 
|  | 96 | +	@Entity(name = "EntityWithIdentity") | 
|  | 97 | +	@Table( name = "`Table with default values`") | 
|  | 98 | +	private static class EntityWithIdentity { | 
|  | 99 | +		private static final String PREFIX = "Entity: "; | 
|  | 100 | +		@Id | 
|  | 101 | +		@GeneratedValue(strategy = GenerationType.IDENTITY) | 
|  | 102 | +		Long id; | 
|  | 103 | + | 
|  | 104 | +		@Column(unique = true) | 
|  | 105 | +		String name; | 
|  | 106 | + | 
|  | 107 | +		@Column(name = "position") | 
|  | 108 | +		@ColumnTransformer(forColumn = "position", write = "? * 2") | 
|  | 109 | +		private int position; | 
|  | 110 | + | 
|  | 111 | +		public EntityWithIdentity() { | 
|  | 112 | +		} | 
|  | 113 | + | 
|  | 114 | +		public EntityWithIdentity(int index) { | 
|  | 115 | +			this.name =  PREFIX + index; | 
|  | 116 | +			this.position = index; | 
|  | 117 | +		} | 
|  | 118 | + | 
|  | 119 | +		public int getPosition() { | 
|  | 120 | +			return position; | 
|  | 121 | +		} | 
|  | 122 | + | 
|  | 123 | +		public void setPosition(int position) { | 
|  | 124 | +			this.position = position; | 
|  | 125 | +		} | 
|  | 126 | + | 
|  | 127 | +		@Override | 
|  | 128 | +		public String toString() { | 
|  | 129 | +			return id + ":" + name + ":" + position; | 
|  | 130 | +		} | 
|  | 131 | +	} | 
|  | 132 | +} | 
0 commit comments