我正在将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
,并且可以使用。
我不确定这是否是问题所在,但任何帮助将不胜感激,并在此先感谢您!