Skip to content

Select projection on arbirtraty POJO

xcesco edited this page Apr 17, 2020 · 2 revisions

In the ORM module, the DAO interfaces allow managing the POJO associated with the DAO. Moreover is possible to define some queries that return another type of POJO. There are two constraints:

  • The POJO has to be marked with @BindSqlType annotation
  • The POJO doesn't have its DAO.

An example:

@BindDao(Loan.class)
public interface LoanDao {

  @BindSqlSelect
  LiveData<List<Loan>> findAllLoans();

  @BindSqlSelect(jql = "SELECT Loan.id, Book.title as bookTitle, User.name as userName, Loan.startTime, "+
     "Loan.endTime From Loan " + 
     "INNER JOIN Book ON Loan.bookId = Book.id "+
     "INNER JOIN User ON Loan.userId = User.id ")
  List<LoanWithUserAndBook> findAllWithUserAndBook();

  @BindSqlSelect(jql = "SELECT "+
      "Loan.id, Book.title as bookTitle, User.name as userName, Loan.startTime, Loan.endTime " + 
      "FROM Book INNER JOIN Loan ON Loan.bookId = Book.id " +
      "INNER JOIN User on User.id = Loan.userId WHERE User.name LIKE :userName " +
      "AND Loan.endTime > :after ")
  LiveData<List<LoanWithUserAndBook>> findLoansByNameAfter(String userName, Date after);

  @BindSqlInsert
  void insertLoan(Loan loan);

  @BindSqlDelete
  void deleteAll();
}

The DAO manages Loan bean. Thus, the queries LoanWithUserAndBook and findLoansByNameAfter allows obtaining LoanWithUserAndBook POJOs

@BindSqlType
public class LoanWithUserAndBook {
    public String id;

    @BindSqlColumn("title")
    public String bookTitle;

    @BindSqlColumn("name")
    public String userName;

    public Date startTime;

    public Date endTime;
}

Table of Contents

Query definition

Features

Relations

Multithread supports

Modularization

Annotations for data convertion

Annotations for SQLite ORM

Annotations for shared preferences

Clone this wiki locally