Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "JNoSQL/Artemis/CRUD"

(Created page with "== CrudRepisotry == In addition to repositories class, Artemis has the CRUDRepository. This interface helps the Entity repository to save, update, delete and retrieve informa...")
 
(Search information from CrudRepository)
Line 65: Line 65:
 
==== Search information from CrudRepository ====
 
==== Search information from CrudRepository ====
  
The CRUDRepository also has a dynamic query from the method name. These are the keywords:
+
The CRUDRepository also has a method query. These are the keywords:
  
  
Line 72: Line 72:
 
Also the operators:
 
Also the operators:
  
* AND
+
* And
* OR
+
* Or
 
* Between
 
* Between
 
* LessThan
 
* LessThan

Revision as of 14:05, 26 April 2017

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.

 interface PersonRepository extends CrudRepository<Person> {

    }

The qualifier is mandatory to define the database type whose will use at the injection point moment.

@Inject
@Database(DatabaseType.DOCUMENT)
private PersonRepository documentRepository;
@Inject
@Database(DatabaseType.COLUMN)
private PersonRepository columnRepository;


And then, as the repository class, create either a ColumnFamilyManager or a DocumentCollectionManager with produces method:


@Produces
public DocumentCollectionManager getManager() {
DocumentCollectionManager manager = //instance
return manager;
}

@Produces
public ColumnFamilyManager getManager() {
ColumnFamilyManager manager = //instance
return manager;
}

So, Artemis will inject automatically.

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);


Search information from CrudRepository

The CRUDRepository 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


interface PersonRepository extends CrudRepository<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);
}

Using CrudRepository as asynchronous way

The CrudRepositoryAsync interface works similarly as CrudRepository but with asynchronous work.


@Inject
@Database(DatabaseType.DOCUMENT)
private PersonRepositoryAsync documentRepositoryAsync;

@Inject
@Database(DatabaseType.COLUMN)
private PersonRepositoryAsync columnRepositoryAsync;

In other words, just inject and then create an Entity Manager async with producers method.


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);

Also, delete and retrieve information with a callback.

    interface PersonRepositoryAsync extends CrudRepositoryAsync<Person> {

        void findByNickname(String nickname, Consumer<List<Person>> callback);

        void deleteByNickName(String nickname);

        void deleteByNickName(String nickname, Consumer<Void> callback);
    }

KeyValueCrudRepository

The KeyValueCrudRepository is a CRUDRepository to key-value type. If the same way of CrudRepository, just extends KeyValueCrudRepository.

 
public interface UserRepository extends KeyValueCrudRepository<User> {
}

And inject the resource.

@Inject
private UserRepository userRepository;


Then use a producer to BucketManager

@Produces
public BucketManager getManager() {
BucketManager manager =//instance
return manager;
}



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"));


Links

Back to the top