|
|
(One intermediate revision by the same user not shown) |
Line 1: |
Line 1: |
− | == CrudRepisotry ==
| |
| | | |
− | In addition to repositories class, Artemis has the CRUDRepository. This interface helps the Entity repository to save, update, delete and retrieve information.
| |
− |
| |
− |
| |
− | To use CrudRepository, just need to create a new interface that extends the '''CrudRepository'''.
| |
− |
| |
− | <pre lang="java">
| |
− | interface PersonRepository extends Repository<Person> {
| |
− |
| |
− | }
| |
− | </pre>
| |
− |
| |
− | The qualifier is mandatory to define the database type whose will use at the injection point moment.
| |
− |
| |
− | <pre lang="java">
| |
− | @Inject
| |
− | @Database(DatabaseType.DOCUMENT)
| |
− | private PersonRepository documentRepository;
| |
− | @Inject
| |
− | @Database(DatabaseType.COLUMN)
| |
− | private PersonRepository columnRepository;
| |
− |
| |
− | </pre>
| |
− |
| |
− |
| |
− | And then, as the repository class, create either a ColumnFamilyManager or a DocumentCollectionManager with produces method:
| |
− |
| |
− |
| |
− | <pre lang="java">
| |
− | @Produces
| |
− | public DocumentCollectionManager getManager() {
| |
− | DocumentCollectionManager manager = //instance
| |
− | return manager;
| |
− | }
| |
− |
| |
− | @Produces
| |
− | public ColumnFamilyManager getManager() {
| |
− | ColumnFamilyManager manager = //instance
| |
− | return manager;
| |
− | }
| |
− |
| |
− | </pre>
| |
− |
| |
− | So, Artemis will inject automatically.
| |
− |
| |
− | <pre lang="java">
| |
− | PersonRepository repository = //instance
| |
− |
| |
− | Person person = new Person();
| |
− | person.setNickname("diana");
| |
− | person.setName("Diana Goodness");
| |
− |
| |
− | List<Person> people = Collections.singletonList(person);
| |
− |
| |
− | repository.save(person);
| |
− | repository.save(people);
| |
− | repository.save(people, Duration.ofHours(2));
| |
− | repository.update(person);
| |
− | repository.update(people);
| |
− | repository.update(people);
| |
− | </pre>
| |
− |
| |
− |
| |
− | ==== Search information from Repository ====
| |
− |
| |
− | The Repository also has a method query. These are the keywords:
| |
− |
| |
− |
| |
− | * '''findBy''': The prefix to find some information
| |
− | * '''deleteBy''': The prefix to delete some information
| |
− | Also the operators:
| |
− |
| |
− | * And
| |
− | * Or
| |
− | * Between
| |
− | * LessThan
| |
− | * GreaterThan
| |
− | * LessThanEqual
| |
− | * GreaterThanEqual
| |
− | * Like
| |
− | * OrderBy
| |
− | * OrderBy____Desc
| |
− | *OrderBy_____ASC
| |
− |
| |
− |
| |
− |
| |
− | <pre lang="java">
| |
− | interface PersonRepository extends Repository<Person> {
| |
− |
| |
− | List<Person> findByAddress(String address);
| |
− |
| |
− | Stream<Person> findByName(String name);
| |
− |
| |
− | Stream<Person> findByNameOrderByNameAsc(String name);
| |
− |
| |
− | Optional<Person> findByNickname(String nickname);
| |
− |
| |
− | void deleteByNickName(String nickname);
| |
− | }
| |
− |
| |
− | </pre>
| |
− |
| |
− | === Using Repository as asynchronous way ===
| |
− |
| |
− | The RepositoryAsync interface works similarly as Repository but with asynchronous work.
| |
− |
| |
− |
| |
− | <pre lang"java">
| |
− | @Inject
| |
− | @Database(DatabaseType.DOCUMENT)
| |
− | private PersonRepositoryAsync documentRepositoryAsync;
| |
− |
| |
− | @Inject
| |
− | @Database(DatabaseType.COLUMN)
| |
− | private PersonRepositoryAsync columnRepositoryAsync;
| |
− | </pre>
| |
− |
| |
− | In other words, just inject and then create an Entity Manager async with producers method.
| |
− |
| |
− |
| |
− | <pre lang="java">
| |
− | PersonRepositoryAsync repositoryAsync = //instance
| |
− |
| |
− | Person person = new Person();
| |
− | person.setNickname("diana");
| |
− | person.setName("Diana Goodness");
| |
− |
| |
− | List<Person> people = Collections.singletonList(person);
| |
− |
| |
− |
| |
− | repositoryAsync.save(person);
| |
− | repositoryAsync.save(people);
| |
− | repositoryAsync.save(person, p -> {});
| |
− | repositoryAsync.save(people, Duration.ofHours(2));
| |
− | repositoryAsync.update(person);
| |
− | repositoryAsync.update(person, p -> {});
| |
− | repositoryAsync.update(people);
| |
− | repositoryAsync.update(people);
| |
− | </pre>
| |
− |
| |
− | Also, delete and retrieve information with a callback.
| |
− |
| |
− | <pre lang="java">
| |
− | interface PersonRepositoryAsync extends RepositoryAsync<Person> {
| |
− |
| |
− | void findByNickname(String nickname, Consumer<List<Person>> callback);
| |
− |
| |
− | void deleteByNickName(String nickname);
| |
− |
| |
− | void deleteByNickName(String nickname, Consumer<Void> callback);
| |
− | }
| |
− | </pre>
| |
− |
| |
− | === KeyValueRepository ===
| |
− |
| |
− | The KeyValueRepository is a Repository to key-value type.
| |
− | If the same way of Repository, just extends '''KeyValueRepository'''.
| |
− |
| |
− | <pre lang="java">
| |
− | public interface UserRepository extends KeyValueRepository<User> {
| |
− | }
| |
− | </pre>
| |
− |
| |
− | And inject the resource.
| |
− | <pre lang="java">
| |
− | @Inject
| |
− | private UserRepository userRepository;
| |
− | </pre>
| |
− |
| |
− |
| |
− | Then use a producer to BucketManager
| |
− |
| |
− | <pre lang="java">
| |
− | @Produces
| |
− | public BucketManager getManager() {
| |
− | BucketManager manager =//instance
| |
− | return manager;
| |
− | }
| |
− | </pre>
| |
− |
| |
− |
| |
− | <pre lang="java">
| |
− |
| |
− | UserRepository userRepository = null;
| |
− | User user = new User("ada", "Ada Lovelace", 30);
| |
− | List<User> users = Collections.singletonList(user);
| |
− | userRepository.put(user);
| |
− | userRepository.put(user, Duration.ofHours(1));
| |
− | userRepository.put(users);
| |
− | userRepository.put(users, Duration.ofHours(1));
| |
− |
| |
− | Optional<User> userOptional = userRepository.get("ada");
| |
− | Iterable<User> usersFound = userRepository.get(Collections.singletonList("ada"));
| |
− |
| |
− | </pre>
| |
− |
| |
− |
| |
− | === Links ===
| |
− |
| |
− | * Documentation https://www.gitbook.com/book/jnosql/jnosql-book/details
| |
− | * [https://wiki.eclipse.org/JNoSQL/Artemis/Start Start with Artemis]
| |