Cassandra:使用cassandra时,Java类抛出InvalidDataAccessApiUsageException

时间:2019-06-05 16:27:52

标签: java spring-boot cassandra

我正在将Spring Boot与Cassandra一起使用以保存数据。但是我的数据库模型无法使用Cassandra的注释。

我将Java 11和Cassandra 3.11.4用于我的Web服务。

CasDBNode


@ToString
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Table("node")
public class CasDBNode{

    @PrimaryKeyColumn(ordinal = 0, name = "id", type = PrimaryKeyType.PARTITIONED)
    private String id;

    @PrimaryKeyColumn(ordinal = 1, name = "config_id", type = PrimaryKeyType.PARTITIONED)
    private String configId;

    @Column
    private String name;

    @Column
    private NodeType type;

    @Column
    private List<? extends NodeResponse> responses;

    @Column("parent_node")
    private String parentNode;

    @Column
    private String conditions;

    @Column("previous_sibling")
    private String previousSibling;

    @Column("api_url")
    private String apiUrl;

    @Column
    private List<NodeQuestion> questions;

}

SimpleNodeQuestion

@ToString
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@UserDefinedType
public class SimpleNodeQuestion implements NodeQuestion {

    private String title;

    private String output;

    @Column("context_name")
    private String contextName;

    private String condition;

    @Column("option_api")
    private String optionApi;

    @Column("default_options")
    private List<Option> defaultOptions;

}

我看到一些解决方案,添加了@UserDefineType应该可以,但仍然出现错误

Cannot resolve reference to bean 'cassandraTemplate' while setting bean property 'cassandraTemplate'; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cassandraTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'cassandraTemplate' parameter 0; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraSession' defined in class path resource [org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Only primitive types are allowed inside Collections for property [questions] of type ['interface java.util.List'] in entity [com.jamesaq12wsx.IBS_Backend.model.db.CasDBNode]

更新

搜索解决方案后,我更改了注释并更改了错误消息

CasDBNode

@ToString
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Table(value = "node")
public class CasDBNode extends CasDBBaseData implements DBNode{

    @PrimaryKeyColumn(ordinal = 0, name = "id", type = PrimaryKeyType.PARTITIONED)
    private String id;

    @PrimaryKeyColumn(ordinal = 1, name = "config_id", type = PrimaryKeyType.PARTITIONED)
    private String configId;

    @Column
    private String name;

    @Column
    private NodeType type;

    @Column
    private List<? extends NodeResponse> responses;

    @Column("parent_node")
    private String parentNode;

    @Column
    private String conditions;

    @Column("previous_sibling")
    private String previousSibling;

    @Column("api_url")
    private String apiUrl;

    @CassandraType(type = DataType.Name.LIST, typeArguments = DataType.Name.UDT, userTypeName = "question")
    private List<DBNodeQuestion> questions;
}

CasDBNodeQuestion

@UserDefinedType(value = "question")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class CasDBNodeQuestion implements DBNodeQuestion {

    private String title;

    private String output;

    private String contextName;

    private String condition;

    private String optionApi;

    @CassandraType(type = DataType.Name.LIST, userTypeName = "option")
    private List<DBOption> defaultOptions;

}

错误消息更改为

nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cassandraTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'cassandraTemplate' parameter 0; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraSession' defined in class path resource [org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.class]: Invocation of init method failed; 
nested exception is org.springframework.data.mapping.MappingException: User type [question] not found

更新

我将interface更改为class,并且可以使用。

我不确定这是否是问题所在,但任何帮助将不胜感激,并在此先感谢您!

0 个答案:

没有答案