Search in sources :

Example 11 with CassandraMappingContext

use of org.springframework.data.cassandra.core.mapping.CassandraMappingContext in project spring-data-cassandra by spring-projects.

the class SchemaTestUtils method createTableAndTypes.

/**
 * Create a table and UDTs for {@code entityClass} if it not exists.
 *
 * @param entityClass must not be {@literal null}.
 * @param operations must not be {@literal null}.
 */
public static void createTableAndTypes(Class<?> entityClass, CassandraOperations operations) {
    CassandraMappingContext mappingContext = operations.getConverter().getMappingContext();
    CassandraPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entityClass);
    SchemaFactory schemaFactory = new SchemaFactory(operations.getConverter());
    potentiallyCreateUdtFor(persistentEntity, operations, schemaFactory);
    potentiallyCreateTableFor(persistentEntity, operations, schemaFactory);
}
Also used : SchemaFactory(org.springframework.data.cassandra.core.convert.SchemaFactory) CassandraMappingContext(org.springframework.data.cassandra.core.mapping.CassandraMappingContext)

Example 12 with CassandraMappingContext

use of org.springframework.data.cassandra.core.mapping.CassandraMappingContext in project spring-boot by spring-projects.

the class CassandraDataAutoConfigurationTests method entityScanShouldSetInitialEntitySet.

@Test
@SuppressWarnings("unchecked")
void entityScanShouldSetInitialEntitySet() {
    load(EntityScanConfig.class);
    CassandraMappingContext mappingContext = this.context.getBean(CassandraMappingContext.class);
    Set<Class<?>> initialEntitySet = (Set<Class<?>>) ReflectionTestUtils.getField(mappingContext, "initialEntitySet");
    assertThat(initialEntitySet).containsOnly(City.class);
}
Also used : Set(java.util.Set) CassandraMappingContext(org.springframework.data.cassandra.core.mapping.CassandraMappingContext) Test(org.junit.jupiter.api.Test)

Example 13 with CassandraMappingContext

use of org.springframework.data.cassandra.core.mapping.CassandraMappingContext in project spring-boot by spring-projects.

the class CassandraReactiveDataAutoConfigurationTests method entityScanShouldSetInitialEntitySet.

@Test
@SuppressWarnings("unchecked")
void entityScanShouldSetInitialEntitySet() {
    load(EntityScanConfig.class, "spring.data.cassandra.keyspaceName:boot_test");
    CassandraMappingContext mappingContext = this.context.getBean(CassandraMappingContext.class);
    Set<Class<?>> initialEntitySet = (Set<Class<?>>) ReflectionTestUtils.getField(mappingContext, "initialEntitySet");
    assertThat(initialEntitySet).containsOnly(City.class);
}
Also used : Set(java.util.Set) CassandraMappingContext(org.springframework.data.cassandra.core.mapping.CassandraMappingContext) Test(org.junit.jupiter.api.Test)

Example 14 with CassandraMappingContext

use of org.springframework.data.cassandra.core.mapping.CassandraMappingContext in project spring-boot by spring-projects.

the class CassandraDataAutoConfiguration method cassandraMapping.

@Bean
@ConditionalOnMissingBean
public CassandraMappingContext cassandraMapping(BeanFactory beanFactory, CassandraCustomConversions conversions) throws ClassNotFoundException {
    CassandraMappingContext context = new CassandraMappingContext();
    List<String> packages = EntityScanPackages.get(beanFactory).getPackageNames();
    if (packages.isEmpty() && AutoConfigurationPackages.has(beanFactory)) {
        packages = AutoConfigurationPackages.get(beanFactory);
    }
    if (!packages.isEmpty()) {
        context.setInitialEntitySet(CassandraEntityClassScanner.scan(packages));
    }
    context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
    return context;
}
Also used : CassandraMappingContext(org.springframework.data.cassandra.core.mapping.CassandraMappingContext) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean) SessionFactoryFactoryBean(org.springframework.data.cassandra.config.SessionFactoryFactoryBean)

Example 15 with CassandraMappingContext

use of org.springframework.data.cassandra.core.mapping.CassandraMappingContext in project spring-data-cassandra by spring-projects.

the class CassandraPersistentEntitySchemaCreatorUnitTests method shouldConsiderProperUdtOrdering.

// DATACASS-687
@Test
void shouldConsiderProperUdtOrdering() {
    List<Class<?>> ordered = new ArrayList<>(Arrays.asList(Udt2.class, Udt1.class, RequiredByAll.class));
    context = new CassandraMappingContext() {

        @Override
        public Collection<CassandraPersistentEntity<?>> getUserDefinedTypeEntities() {
            return ordered.stream().map(this::getRequiredPersistentEntity).collect(Collectors.toList());
        }
    };
    context.setUserTypeResolver(typeName -> {
        // to be created user types isn't a good idea because they do not exist at resolution time.
        throw new IllegalArgumentException(String.format("Type %s not found", typeName));
    });
    CassandraPersistentEntitySchemaCreator schemaCreator = new CassandraPersistentEntitySchemaCreator(context, adminOperations);
    List<CreateUserTypeSpecification> userTypeSpecifications = schemaCreator.createUserTypeSpecifications(false);
    List<CqlIdentifier> collect = userTypeSpecifications.stream().map(UserTypeNameSpecification::getName).collect(Collectors.toList());
    assertThat(collect).hasSize(3).startsWith(CqlIdentifier.fromCql("requiredbyall"));
}
Also used : CreateUserTypeSpecification(org.springframework.data.cassandra.core.cql.keyspace.CreateUserTypeSpecification) ArrayList(java.util.ArrayList) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Collection(java.util.Collection) CassandraMappingContext(org.springframework.data.cassandra.core.mapping.CassandraMappingContext) Test(org.junit.jupiter.api.Test)

Aggregations

CassandraMappingContext (org.springframework.data.cassandra.core.mapping.CassandraMappingContext)29 BeforeEach (org.junit.jupiter.api.BeforeEach)10 Test (org.junit.jupiter.api.Test)8 MappingCassandraConverter (org.springframework.data.cassandra.core.convert.MappingCassandraConverter)8 SimpleUserTypeResolver (org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver)5 ArrayList (java.util.ArrayList)4 Bean (org.springframework.context.annotation.Bean)4 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)3 ApplicationContext (org.springframework.context.ApplicationContext)3 ReactiveEntityCallbacks (org.springframework.data.mapping.callback.ReactiveEntityCallbacks)3 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)2 Collection (java.util.Collection)2 Set (java.util.Set)2 ApplicationScoped (javax.enterprise.context.ApplicationScoped)2 Produces (javax.enterprise.inject.Produces)2 SessionFactoryFactoryBean (org.springframework.data.cassandra.config.SessionFactoryFactoryBean)2 CassandraAdminTemplate (org.springframework.data.cassandra.core.CassandraAdminTemplate)2 SchemaFactory (org.springframework.data.cassandra.core.convert.SchemaFactory)2 CreateUserTypeSpecification (org.springframework.data.cassandra.core.cql.keyspace.CreateUserTypeSpecification)2 UserTypeResolver (org.springframework.data.cassandra.core.mapping.UserTypeResolver)2