gpt4 book ai didi

java - 使用 datastax cassandra 映射器时出现 NoSuchMethodException

转载 作者:行者123 更新时间:2023-12-01 09:13:04 25 4
gpt4 key购买 nike

我刚刚开始尝试使用 cassandra 和 java,以及 spring REST 库。

但是,当我尝试从 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]

这就是我使用映射器的方式

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

最佳答案

哦,我现在明白问题了

您需要添加一个构造函数

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

关于java - 使用 datastax cassandra 映射器时出现 NoSuchMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40796428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com