EzyJPA: Query data

1. Create a Repsitory

If you have no idea about EzyJPA, please take a look the introduction. And now, let's say we have a Book class like this:
@Entity
public class Book extends CommonEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private Long categoryId;
    private Long authorId;
    private String name;
    private BigDecimal price;
    private LocalDate releaseDate;
    private LocalDateTime releaseTime;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
}
To create a repository, you just need add a repository like this:
@EzyRepository
public interface BookRepository extends EzyDatabaseRepository<Long, Book> {}

2. Available methods

EzyJPA contains some method:
  1. save: to save one or many entities
  2. delete: to delete one or many entities
  3. find: to entities by id, ids, by field or find all
  4. count: count all entities
example:
bookRepository.save(book);

bookRepository.delete(bookId);

Book book = bookRepository.findById(bookId);

bookRepository.count()
For a completed example, you can look at here.

3. Custom method

EzyJPA supports you create a custom method to query SQL database. Let's say, we want to find a book by name and authorId, we can do like this:
Book findByNameAndAuthorId(String name, Long authorId);
In this method, Name is Book's name field and AuthorId is Book's authorId field. It means you can use this template to create a lot of query methods, like:
List<Book> findByCategoryIdOrAuthorId(long category, long authorId);

List<Book> findByCategoryIdAndName(long category, long authorId, Next next);

int countByAuthorId(long authorId);

4. Custom query method

If you have a complicated query and can not create a custom method, you can annotate your method with @EzyQuery annotation like this:
@EzyQuery("select e from Book e order by e.price desc, e.id desc")
List<Book> findBooksOrderByPriceAndId(Next next);

@EzyQuery(
    value = "select * from book e " +
        "where e.price > ?0 or (e.price = ?0 and e.id > ?1) " +
        "order by e.price desc, e.id desc",
    nativeQuery = true
)
List<Book> findBooks(
    BigInteger priceExclusive,
    long idExclusive,
    Next next
);

@EzyQuery("select e from Book e where e.name < ?0 order by e.name")
List<Book> findByNameLt(String name, Next next);

@EzyQuery("select e from Book e where e.name > ?0 order by e.name")
List<Book> findByNameGt(String name, Next next);

@EzyQuery(value = "select sum(e.price) as sum from Book e", nativeQuery = true)
SumBookPriceResult sumPrice();

4. Pagination

EzyJPA use Next class to help you specific skip and limit value, you can create Next by:
Next.fromSkipLimit(skip, limit)

// or
Next.fromPageSize(page, size)
Full source code of examples is available on Github

Next

You can take a look how to use custom query result