Provides Spring Data approach to work with Reindexer database.
<dependency>
<groupId>io.github.evgeniycheban</groupId>
<artifactId>spring-data-reindexer</artifactId>
<version>${spring-data-reindexer.version}</version>
</dependency>
Here is an example of basic spring-data-reindexer
usage:
import ru.rt.restream.reindexer.Reindexer;
import ru.rt.restream.reindexer.ReindexerConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.reindexer.repository.config.EnableReindexerRepositories;
@Configuration
@EnableReindexerRepositories
public class ReindexerConfig {
@Bean
public Reindexer reindexer() {
return ReindexerConfiguration.builder()
.url("cproto://localhost:6534/items")
.getReindexer();
}
}
import ru.rt.restream.reindexer.annotations.Reindex;
import org.springframework.data.reindexer.core.mapping.Namespace;
@Namespace(name = "items")
public class Item {
@Reindex(name = "id", isPrimaryKey = true)
private Long id;
@Reindex(name = "name")
private String name;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.Optional;
import org.springframework.data.reindexer.repository.ReindexerRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ItemRepository extends ReindexerRepository<Item, Long> {
Optional<Item> findByName(String name);
}
The ItemRepository
can be injected into spring-managed beans using @Autowired
or
another Spring Framework approach.
@Autowired
private ItemRepository repository;
More examples can be found here.
Transactions are implemented using @Transactional
annotation approach, to enable
transaction management support @EnableTransactionManagement
annotation should be placed
on the @Configuration
class and ReindexerTransactionManager
bean should be defined in
the context.
Note that ReindexerTransactionManager
manages transactions for a single namespace,
therefore it should be defined with a domain class that is mapped to a Reindexer
namespace.
Here is an example of basic transaction management usage:
import ru.rt.restream.reindexer.Reindexer;
import ru.rt.restream.reindexer.ReindexerConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.reindexer.ReindexerTransactionManager;
import org.springframework.data.reindexer.repository.config.EnableReindexerRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableReindexerRepositories
@EnableTransactionManagement
public class TransactionalReindexerConfig {
@Bean
public Reindexer reindexer() {
return ReindexerConfiguration.builder()
.url("cproto://localhost:6534/items")
.getReindexer();
}
@Bean
public ReindexerTransactionManager<Item> itemTxManager(Reindexer reindexer) {
return new ReindexerTransactionManager<>(reindexer, Item.class);
}
}
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class ItemTransactionalService {
private final ItemRepository repository;
public ItemTransactionalService(ItemRepository repository) {
this.repository = repository;
}
@Transactional(transactionManager = "itemTxManager")
public Item save(Item item) {
return this.repository.save(item);
}
}
The @Query
annotation is used to declare SQL-based Reindexer queries
directly on repository methods.
Parameter binding supports named parameters using @Param
annotation as well
as parameter number links.
@Query("SELECT * FROM items WHERE name = :name")
Optional<TestItem> findOneByName(@Param("name") String name);
@Query("SELECT * FROM items WHERE name = ?1")
Optional<TestItem> findOneByName(String name);
- Support more return types for
ReindexerRepository
.