Search in sources :

Example 21 with CassandraMappingContext

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

the class MultipackageScanningUnitTests method before.

@BeforeEach
void before() throws ClassNotFoundException {
    CassandraMappingContext context = new CassandraMappingContext();
    context.setInitialEntitySet(CassandraEntityClassScanner.scan(pkg + ".first", pkg + ".second"));
    context.initialize();
    this.context = context;
}
Also used : CassandraMappingContext(org.springframework.data.cassandra.core.mapping.CassandraMappingContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 22 with CassandraMappingContext

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

the class CassandraConfig method mappingContext.

@Bean
public CassandraMappingContext mappingContext(CqlSession cqlSession) {
    CassandraMappingContext mappingContext = new CassandraMappingContext();
    mappingContext.setUserTypeResolver(new SimpleUserTypeResolver(cqlSession));
    return mappingContext;
}
Also used : SimpleUserTypeResolver(org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver) CassandraMappingContext(org.springframework.data.cassandra.core.mapping.CassandraMappingContext) Bean(org.springframework.context.annotation.Bean) SessionFactoryFactoryBean(org.springframework.data.cassandra.config.SessionFactoryFactoryBean) CqlSessionFactoryBean(org.springframework.data.cassandra.config.CqlSessionFactoryBean)

Example 23 with CassandraMappingContext

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

the class NamingStrategyConfiguration method configurationMethod.

public void configurationMethod() {
    // tag::method[]
    CassandraMappingContext context = new CassandraMappingContext();
    // default naming strategy
    context.setNamingStrategy(NamingStrategy.INSTANCE);
    // snake_case converted to upper case (SNAKE_CASE)
    context.setNamingStrategy(NamingStrategy.SNAKE_CASE.transform(String::toUpperCase));
// end::method[]
}
Also used : CassandraMappingContext(org.springframework.data.cassandra.core.mapping.CassandraMappingContext)

Example 24 with CassandraMappingContext

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

the class CassandraOperationsProducer method createCassandraOperations.

@Produces
@ApplicationScoped
public CassandraOperations createCassandraOperations(CqlSession session) throws Exception {
    CassandraMappingContext mappingContext = new CassandraMappingContext();
    mappingContext.setUserTypeResolver(new SimpleUserTypeResolver(session, CqlIdentifier.fromCql(KEYSPACE_NAME)));
    mappingContext.setInitialEntitySet(Collections.singleton(User.class));
    mappingContext.afterPropertiesSet();
    MappingCassandraConverter cassandraConverter = new MappingCassandraConverter(mappingContext);
    CassandraAdminTemplate cassandraTemplate = new CassandraAdminTemplate(session, cassandraConverter);
    CreateKeyspaceSpecification createKeyspaceSpecification = CreateKeyspaceSpecification.createKeyspace(KEYSPACE_NAME).ifNotExists();
    cassandraTemplate.getCqlOperations().execute(CreateKeyspaceCqlGenerator.toCql(createKeyspaceSpecification));
    cassandraTemplate.getCqlOperations().execute("USE " + KEYSPACE_NAME);
    CassandraPersistentEntitySchemaDropper schemaDropper = new CassandraPersistentEntitySchemaDropper(mappingContext, cassandraTemplate);
    schemaDropper.dropTables(false);
    schemaDropper.dropUserTypes(false);
    CassandraPersistentEntitySchemaCreator schemaCreator = new CassandraPersistentEntitySchemaCreator(mappingContext, cassandraTemplate);
    schemaCreator.createUserTypes(false);
    schemaCreator.createTables(false);
    for (CassandraPersistentEntity<?> entity : cassandraTemplate.getConverter().getMappingContext().getTableEntities()) {
        cassandraTemplate.truncate(entity.getType());
    }
    return cassandraTemplate;
}
Also used : CassandraPersistentEntitySchemaDropper(org.springframework.data.cassandra.core.CassandraPersistentEntitySchemaDropper) User(org.springframework.data.cassandra.domain.User) CassandraAdminTemplate(org.springframework.data.cassandra.core.CassandraAdminTemplate) CreateKeyspaceSpecification(org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification) SimpleUserTypeResolver(org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver) CassandraPersistentEntitySchemaCreator(org.springframework.data.cassandra.core.CassandraPersistentEntitySchemaCreator) CassandraMappingContext(org.springframework.data.cassandra.core.mapping.CassandraMappingContext) MappingCassandraConverter(org.springframework.data.cassandra.core.convert.MappingCassandraConverter) Produces(javax.enterprise.inject.Produces) ApplicationScoped(javax.enterprise.context.ApplicationScoped)

Example 25 with CassandraMappingContext

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

the class CassandraNamespaceIntegrationTests method mappingContextShouldHaveUserTypeResolverConfigured.

// DATACASS-172
@Test
void mappingContextShouldHaveUserTypeResolverConfigured() {
    CassandraMappingContext mappingContext = this.applicationContext.getBean(CassandraMappingContext.class);
    SimpleUserTypeResolver userTypeResolver = (SimpleUserTypeResolver) ReflectionTestUtils.getField(mappingContext, "userTypeResolver");
    assertThat(userTypeResolver).isNotNull();
}
Also used : SimpleUserTypeResolver(org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver) CassandraMappingContext(org.springframework.data.cassandra.core.mapping.CassandraMappingContext) AbstractSpringDataEmbeddedCassandraIntegrationTest(org.springframework.data.cassandra.repository.support.AbstractSpringDataEmbeddedCassandraIntegrationTest) 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