UserTypeResolver must not be null

I try to check the Cassandra UDT insert and I continue to encounter the following error: Exception in thread "main" java.lang.IllegalArgumentException: UserTypeResolver must not be null

After I tried to figure out my own path, I tried to accurately reproduce the approach described below: Custom type with spring -data-cassandra

However, I still get the same error.

I can insert into the target database when I delete the UDT and just insert simple types, so I know that I connect accordingly. My configuration is as follows:

@Configuration
@PropertySource(value = { "classpath:cassandra.properties" })
//@EnableCassandraRepositories(basePackages = { "org.spring.cassandra.example.repo" })
public class CassandraConfig {

private static final Logger LOG = LoggerFactory.getLogger(CassandraConfig.class);

@Autowired
private Environment env;

@Bean
public CassandraClusterFactoryBean cluster() {

    CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
    cluster.setContactPoints(env.getProperty("cassandra.contactpoints"));
    cluster.setPort(Integer.parseInt(env.getProperty("cassandra.port")));

    return cluster;
}

@Bean
public CassandraMappingContext mappingContext() {
    BasicCassandraMappingContext mappingContext = new BasicCassandraMappingContext();
    mappingContext.setUserTypeResolver(new SimpleUserTypeResolver(cluster().getObject(), "campaign_management"));
    return mappingContext;
}

@Bean
public CassandraConverter converter() {
    return new MappingCassandraConverter(mappingContext());
}

@Bean
public CassandraSessionFactoryBean session() throws Exception {

    CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
    session.setCluster(cluster().getObject());
    session.setKeyspaceName(env.getProperty("cassandra.keyspace"));
    session.setConverter(converter());
    session.setSchemaAction(SchemaAction.NONE);

    return session;
}

@Bean
public CassandraOperations cassandraTemplate() throws Exception {
    return new CassandraTemplate(session().getObject());
}
}

My “Addresses” and “Employees” classes comply with the above SO recommendations above, and my basic information is simple:

public class MainClass {

public static void main(String[] args) {

ApplicationContext service = new AnnotationConfigApplicationContext(CassandraConfig.class);

Employee employee = new Employee();
employee.setEmployee_id(UUID.randomUUID());
employee.setEmployee_name("Todd");
Address address = new Address();
address.setAddress_type("Home");
address.setId("ToddId");
employee.setAddress(address);
CassandraOperations operations = service.getBean("cassandraTemplate", CassandraOperations.class);

operations.insert(employee);

System.out.println("Done");
}
}

I use:

datastax.cassandra.driver.version=3.1.3
spring.data.cassandra.version=1.5.1
spring.data.commons.version=1.13.1
spring.cql.version=1.5.1

, SO, 1.5.0, spring.io 1.5.1 , , 1.5.0.

, .

+4
1

, UserTypeResolver cassandra, cassandra, Spring Data Cassandra

:

, Spring MVC ...

UserDefinedTypes Cassandra SET MAP, .

Spring Bean (Spring XML-):

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:cassandra="http://www.springframework.org/schema/data/cassandra"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:task="http://www.springframework.org/schema/task"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
   http://www.springframework.org/schema/data/cassandra http://www.springframework.org/schema/data/cassandra/spring-cassandra.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
...
<!-- ===== CASSANDRA ===== -->
<!-- Loads the properties into the Spring Context and uses them to fill in placeholders in bean definitions below -->
<context:property-placeholder location="/WEB-INF/spring/cassandra.properties" />

<!-- REQUIRED: The Cassandra Cluster -->
<cassandra:cluster contact-points="${cassandra.contactpoints}"
    port="${cassandra.port}" username="cassandra" password="cassandra"
    auth-info-provider-ref="authProvider" />

<!-- REQUIRED: The Cassandra Session, built from the Cluster, and attaching to a keyspace -->
<cassandra:session keyspace-name="${cassandra.keyspace}" />

<!-- REQUIRED: The Default Cassandra Mapping Context used by CassandraConverter 
     DO include a userTypeResolver for UDT support -->
<cassandra:mapping entity-base-packages="fr.woobe.model">
    <cassandra:user-type-resolver keyspace-name="${cassandra.keyspace}" />
</cassandra:mapping>

<!-- REQUIRED: The Default Cassandra Converter used by CassandraTemplate -->
<cassandra:converter />

<bean id="authProvider" class="com.datastax.driver.core.PlainTextAuthProvider">
    <constructor-arg index="0" value="myCassandraUser" />
    <constructor-arg index="1" value="somePassword" />
</bean>

<!-- REQUIRED: The Cassandra Template is the building block of all Spring Data Cassandra -->
<cassandra:template id="cassandraTemplate" />
...

java, MVC Spring:

import org.springframework.data.cassandra.core.CassandraOperations;
...
// acquire DB template
CassandraOperations cOps = this.beanFactory.getBean("cassandraTemplate", CassandraOperations.class);
// for instance: load everything
List<MyData> rows = cOps.select("SELECT * FROM mydatatable", MyData.class);
// assuming an entry with index i exists...
Set<Pair> mySetOfPairs = rows.get(i).pairSet;
if (mySetOfPairs!=null)
   for (Pair p : mySetOfPairs) { 
        ... handle p.first and p.second ...
...

:

package example.model;
import java.util.Set;
import org.springframework.data.cassandra.core.mapping.CassandraType;
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;
import com.datastax.driver.core.DataType.Name;

@Table public class MyData {
   @PrimaryKey 
   public String myKey;
   // some other basic fields...
   public String moreStuff;
   // a SET of user defined 'pair type'
   @CassandraType(type = Name.SET, userTypeName = "pairType")
   public Set<Pair> pairSet;

   // your constructors and other methods here...
}

, :

package example.model;
import org.springframework.data.cassandra.core.mapping.UserDefinedType;

@UserDefinedType("pairType")
public class Pair {
   public String first;
   public String second;

   public Pair() {
   }
   public Pair(String f, String s) {
    this.first= f;
    this.second= s;
   }
}

Cassandra, :

CREATE TYPE pairType (first text, second text);

CREATE TABLE MyData (
  myKey text,
  moreStuff text,
  pairSet set<frozen<pairType>>,
  PRIMARY KEY (myKey)
  ) ;

INSERT INTO MyData (myKey, moreStuff, pairSet) 
  VALUES ('hello', 'world', { 
                  { first:'one', second:'two' }, 
                  { first:'out', second:'there' } } 
         ) ;

Maven, spring -webmvc , Web MVC Spring, spring -context-support spring -data-cassandra. dataStax cassandra .

+1

Source: https://habr.com/ru/post/1674430/


All Articles