Reputation: 162
I've seen people saying its possible like in here.
But there is no implementation, only definition:
@Repository
public interface StudentRepository extends CrudRepository<Student, String>{
Student findByNameAndGender(String name, Gender gender);
}
I've followed the structure used here, where they have:
public interface UserRepository {
void save(User user);
Map<String,User> findAll();
User findById(String id);
void update(User user);
void delete(String id);
}
They don't extend the Repository with CrudRepository
:
extends CrudRepository<Student, String>
And they have the implemented separately like so:
@Repository
public class UserRepositoryImpl implements UserRepository {
private RedisTemplate < String, User > redisTemplate;
private HashOperations hashOperations; //to access Redis cache
public UserRepositoryImpl(RedisTemplate<String, User> redisTemplate) {
this.redisTemplate = redisTemplate;
hashOperations = redisTemplate.opsForHash();
}
@Override
public void save(User user) {
hashOperations.put("USER", user.getId(), user);
}
...
So can you give me an example of an implementation of findByNameAndGender(String name, Gender gender)?
Upvotes: 1
Views: 2082
Reputation: 91
I implemented it according to the example below.
@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false)
@NoArgsConstructor
@RedisHash(value = "parameter")
public class CustomRedisParameter implements Serializable {
private static final long serialVersionUID = 5468738363087043202L;
@Id
private Long id;
@Indexed
private ParameterKey key;
@Indexed
private Long cityId;
}
public class RedisParameterService {
private final RedisParameterRepository repository;
@Autowired
public RedisParameterService(RedisParameterRepository repository) {
this.repository = repository;
}
@Transactional
public void save(CustomRedisParameter parameter) {
repository.save(parameter);
}
}
@Repository
public interface RedisParameterRepository extends
CrudRepository<CustomRedisParameter, Long> {
Optional<CustomRedisParameter>
findByKeyAndCityId(ParameterKey key, Long cityId);
}
Upvotes: 1