使用datastax cassandra映射器的NoSuchMethodException

时间:2016-11-25 01:15:19

标签: java cassandra datastax

我刚刚开始使用Spring REST库来尝试使用cassandra和java。

但是当我试图从cassandra获取对象时我收到此错误我收到此错误

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]

这就是我使用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;
}

并且是对象

@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;
}

我错过了什么?

编辑:

继承人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>

1 个答案:

答案 0 :(得分:1)

哦,我现在看到了问题

您需要添加构造函数

@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;
}
}