如何提供CassandraEntityInformation对于 SimpleReactiveCassandraRepository?
我正在对 spring-data-cassandra-reactive 进行一些研究,看起来我无法弄清楚或找到关于此的正确文档。
根据文档,您可以创建一个用 @Repository
注释的接口
,然后使用注释创建自定义查询来检索您想要的数据。代码看起来像这样:
@Repository
public interface UserRepository
extends ReactiveCassandraRepository<UserEntity, UUID>
{
@Query("SELECT u FROM UserEntity u WHERE username = :user ALLOW FILTERING")
Mono<UserEntity> findUserEntityByUsername(@Param("user") String user);
/**
* This is just to illustrate/simulate some custom/advanced logic that cannot be
* done via @Query()
*/
default Mono<UserEntity> saveWithBase64EncodedPassword(UserEntity entity)
{
String encodedPassword = Base64.getEncoder().encodeToString(entity.getPassword().getBytes());
entity.updatePassword(encodedPassword);
return this.save(entity);
}
}
@Table(UserEntity.TABLE_NAME)
public class UserEntity
{
public final static String TABLE_NAME = "users";
@PrimaryKeyColumn(name = "uuid", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
@CassandraType(type = CassandraType.Name.UUID)
@Column
private UUID id;
@Column
@CassandraType(type = CassandraType.Name.VARCHAR)
private String username;
@Column
@CassandraType(type = CassandraType.Name.VARCHAR)
private String password;
public UserEntity()
{
this.id = UUID.randomUUID();
}
public UserEntity(String username, String password)
{
this.id = UUID.randomUUID();
this.username = username;
this.password = password;
}
public UUID getId()
{
return id;
}
public void setId(UUID id)
{
this.id = id;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public void updatePassword(String newPassword)
{
this.password = newPassword;
}
}
依赖关系:
plugins {
id("org.springframework.boot") version "2.6.6"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
}
dependencies {
// Embedded Cassandra Server - used for testing.
implementation("com.github.nosan:embedded-cassandra-spring-boot-starter:4.1.0")
// Spring Data Cassandra Dependencies
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-data-cassandra-reactive")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude("org.junit.vintage:junit-vintage-engine")
exclude("com.vaadin.external.google:android-json")
}
testImplementation("io.projectreactor:reactor-test")
// /Spring Data Cassandra Dependencies
}
到目前为止,这工作得很好。但是,我试图弄清楚如何切换到实现接口以便能够使用 @Autowire
(即在此示例代码中自动装配
org.springframework.security.crypto.password.PasswordEncoder)
浏览 spring-data-cassandra jar 中的代码我注意到有一个名为 org.springframework 的类.data.cassandra.repository.support.SimpleReactiveCassandraRepository 您可以从中进行扩展。它已经具备了您需要实现的大多数常见功能,这就是为什么它看起来是扩展
的完美候选者。
问题来了 - 它需要 CassandraEntityInformation
有什么想法吗?
I'm doing some research bout spring-data-cassandra-reactive
and it looks like I can't figure out or find the right documentation about this.
According to the documentation you can create an interface
which is annotated with @Repository
and then use annotations to create custom queries that will retrieve the data you want. The code would look something like this:
@Repository
public interface UserRepository
extends ReactiveCassandraRepository<UserEntity, UUID>
{
@Query("SELECT u FROM UserEntity u WHERE username = :user ALLOW FILTERING")
Mono<UserEntity> findUserEntityByUsername(@Param("user") String user);
/**
* This is just to illustrate/simulate some custom/advanced logic that cannot be
* done via @Query()
*/
default Mono<UserEntity> saveWithBase64EncodedPassword(UserEntity entity)
{
String encodedPassword = Base64.getEncoder().encodeToString(entity.getPassword().getBytes());
entity.updatePassword(encodedPassword);
return this.save(entity);
}
}
@Table(UserEntity.TABLE_NAME)
public class UserEntity
{
public final static String TABLE_NAME = "users";
@PrimaryKeyColumn(name = "uuid", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
@CassandraType(type = CassandraType.Name.UUID)
@Column
private UUID id;
@Column
@CassandraType(type = CassandraType.Name.VARCHAR)
private String username;
@Column
@CassandraType(type = CassandraType.Name.VARCHAR)
private String password;
public UserEntity()
{
this.id = UUID.randomUUID();
}
public UserEntity(String username, String password)
{
this.id = UUID.randomUUID();
this.username = username;
this.password = password;
}
public UUID getId()
{
return id;
}
public void setId(UUID id)
{
this.id = id;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public void updatePassword(String newPassword)
{
this.password = newPassword;
}
}
Dependencies:
plugins {
id("org.springframework.boot") version "2.6.6"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
}
dependencies {
// Embedded Cassandra Server - used for testing.
implementation("com.github.nosan:embedded-cassandra-spring-boot-starter:4.1.0")
// Spring Data Cassandra Dependencies
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-data-cassandra-reactive")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude("org.junit.vintage:junit-vintage-engine")
exclude("com.vaadin.external.google:android-json")
}
testImplementation("io.projectreactor:reactor-test")
// /Spring Data Cassandra Dependencies
}
This, so far, works fine. However, I'm trying to figure out how to switch to implementing the interface in order to be able to use @Autowire
(i.e. in this example code to autowireorg.springframework.security.crypto.password.PasswordEncoder
)
Surfing through the code in the spring-data-cassandra
jar I noticed there's a class named org.springframework.data.cassandra.repository.support.SimpleReactiveCassandraRepository
which you can extend from. It already has most of the common things you would need implemented for you which is why it looks like the perfect candidate to extend
from.
And here comes the problem - it requires CassandraEntityInformation<T, ID> metadata
.
I cannot seem to find where this is taken from or how it's supposed to be auto-wired and I feel like I might be missing something or maybe a dependency.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SimpleReactiveCassandraRepository
是一个很棒的类,因为它使您可以访问ReactiveCassandraOperations
以及CqlSession
。这是进行细粒度操作的好方法。 (LWT,Bacthes)您是正确的,不同的类将被自动装配。这是示例代码:
TLDR;我使用 Spring data Reactive 实现了 Spring PetClinic,您可以在此处 这是一个包含代码的分步研讨会。您可能需要特别查看 此文件夹
在项目中,您会发现仅与
驱动程序
反应,与CassandraRepositories
反应以及与SimpleCassandraRepositories
。 (检查TEST文件夹)SimpleReactiveCassandraRepository
is a great class as it gives you access toReactiveCassandraOperations
and as such toCqlSession
. It is a great way to have fine grained operations. (LWT, Bacthes)You are correct the different classes would be autowired. Here is a sample code:
TLDR; I implemented the Spring PetClinic with Spring data Reactive and you can have access to the full code here It is a step by step workshop with the code. You may want to look specially at this folder
In the project you will find reactive with the
drivers only
, reactive withCassandraRepositories
and reactive withSimpleCassandraRepositories
. (Check the TEST folder)