Pita Pita - 16 days ago 4
Java Question

NoSuchMethodException using the datastax cassandra mapper

I've just started experimenting with cassandra and java, with the spring REST libraries.

However I'm getting this error when im trying to get the object back from cassandra I get this error

java.lang.NoSuchMethodException: objects.DataSet.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_111]
at java.lang.Class.getConstructor(Class.java:1825) ~[na:1.8.0_111]
at com.datastax.driver.mapping.ReflectionUtils.newInstance(ReflectionUtils.java:37) ~[cassandra-driver-mapping-3.1.0.jar:na]
at com.datastax.driver.mapping.EntityMapper.newEntity(EntityMapper.java:62) ~[cassandra-driver-mapping-3.1.0.jar:na]


This is how im using the mapper

@RequestMapping(value="/data_sets/{dataSetId}", method=RequestMethod.GET)
public DataSet show(@PathVariable(value="dataSetId") String dataSetId){
Cluster cluster = Cluster.builder().withClusterName("testCluster").addContactPoint("127.0.0.1").build();
Session session = cluster.connect("testks");

MappingManager manager = new MappingManager(session);

Mapper<DataSet> mapper = manager.mapper(DataSet.class);

System.out.println("loading data set ....");
DataSet dataSet = mapper.get(dataSetId);
return dataSet;
}


and heres the object

@Table(keyspace="testks", name="DataSet", caseSensitiveKeyspace=false, caseSensitiveTable=false)
public class DataSet {

@PartitionKey
@Column
String id;
@Column
String name;

public String getName() {
return name;
}

public DataSet setName(String name) {
this.name = name;
return this;
}

public DataSet setId(String id){
this.id = id;
return this;
}

public DataSet(String name){
this.id = UUIDs.timeBased().toString();
this.name = name;
}


what am I missing?

EDIT:

heres the pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Banana</groupId>
<artifactId>Banana</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source />
<target />
</configuration>
</plugin>
</plugins>
</build>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<dependencies>
<!--<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-cassandra</artifactId>
<version>1.4.4.RELEASE</version> </dependency> -->
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-mapping</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-extras</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.2.1.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.2.0</version>
</dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>

<repositories>
<repository>
<id>spring-milestone</id>
<name>Spring Maven MILESTONE Repository</name>
<url>http://repo.spring.io/libs-milestone</url>
</repository>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>



Answer

Oh, I see now the problem

You need to add a constructor

@Table(keyspace="testks", name="DataSet", caseSensitiveKeyspace=false, caseSensitiveTable=false)
public class DataSet {

@PartitionKey
@Column
String id;
@Column
String name;

public Dataset() {
}

public String getName() {
    return name;
}

public DataSet setName(String name) {
    this.name = name;
    return this;
}

public DataSet setId(String id){
    this.id = id;
    return this;
}

public DataSet(String name){
    this.id = UUIDs.timeBased().toString();
    this.name = name;
}
}