我正在尝试构建一些自定义查询,如文档所说,但它不工作。
我被困在我认为是块方法,而不是任何数据被检索。
这是我的repo类:@Repository
public interface UserRepository extends FirestoreReactiveRepository<User> {
Flux<User> findByCPF(String CPF);
Flux<User> findByEmail(String email);
}
模型如下:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Document(collectionName = "userCollection")
public class User extends BaseEntity {
@NotNull(message = "{validation.notnull.user.name}")
private String name;
@Email
@NotNull(message = "{validation.notnull.user.email}")
private String email;
@NotNull(message = "{validation.notnull.user.password}")
private String password;
@NotNull(message = "{validation.notnull.user.address}")
private String address;
@CPF
@NotNull(message = "{validation.notnull.user.cpf}")
private String CPF;
@NotNull(message = "{validation.notnull.user.birthDate}")
private Date birthDate;
@NotNull(message = "{validation.notnull.user.role}")
private UserRoleEnum role;
}
这是我正在构建的方法,我卡住了:
@Override
public Mono<User> create(User entity) {
var cpf = userRepository.findByCPF(entity.getCPF());
var email = userRepository.findByEmail(entity.getEmail());
if(cpf == null || email == null)
throw new EntityExistException();
else
return super.create(entity);
}
是的,这是一个重写,因为我有一个通用的服务类:
public class GenericService<T>{
private FirestoreReactiveRepository<T> firestoreReactiveRepository;
public GenericService(FirestoreReactiveRepository<T> firestoreReactiveRepository){
this.firestoreReactiveRepository = firestoreReactiveRepository;
}
@Transactional
public Mono<T> create(T entity){
return firestoreReactiveRepository.save(entity);
}
**** HIDED CONTENT *****
你知道如何使用FirestoreReactiveRepository来构建自定义查询吗?
有哪些类型:
var cpf = userRepository.findByCPF(entity.getCPF());
var email = userRepository.findByEmail(entity.getEmail());
?
你也可以这样写:
Flux<User> cpf = userRepository.findByCPF(entity.getCPF());
Flux<User> email = userRepository.findByEmail(entity.getEmail());
我看到三个问题:
- 你不同意
cpf
和email
的通量 - if中的条件总是false,因为两个通量都不是null
if(cpf == null || email == null)
- …假设
cpf
和email
不是Flux<User>
而是User
,我认为条件应该是cpf != null || email != null
代码看起来更像这样:
@Override
public Mono<User> create(User entity) {
var cpf = userRepository.findByCPF(entity.getCPF()); // Flux<User> cpf = ...
var email = userRepository.findByEmail(entity.getEmail()); // Flux<User> email = ...
return cpf.mergeWith(email)
.<User>handle((user, sink) -> sink.error(new EntityExistException()))
.switchIfEmpty(super.create(entity))
.next();
}