Skip to content

@BindSqlChildSelect

xcesco edited this page Sep 17, 2021 · 2 revisions

DAOs work with associated entities, so usually it can be used to insert/update/select/delete a specific type of bean. Suppose you have a data source which data model is composed by two entities: album and song. There is a one-2-many relation between them.

@BindTable
public class Album {
  public  long id;
  public String name;

  @BindRelation(foreignKey = "albumId")
  public List<Song> songs;
}
@BindTable
public class Song {
  public long id;
  public String name;
  
  @BindColumn(parentEntity=Album.class)
  public long albumId;
}

albumId is the foreign key of the relation. songs field, marked with @BindRelation, can contains all album's songs and it can not be stored in a table column. Every class is managed by its DAO. If you want, you can link associated DAO to load an album and its songs with the use of child selects.

@BindDao(Album.class)
public interface DaoAlbum extends DaoBase<Album> {
  @BindSqlSelect(childrenSelects={
    @BindSqlChildSelect(relation="songs", method="selectByAlbumId")
  })
  List<Album> selectAlbums();
}

@BindDao(Song.class)
public interface DaoSong extends DaoBase<Song> {

  @BindSqlSelect
  List<Song> selectAll();
	
  @BindSqlSelect(where="albumId=${albumId}")
  List<Song> selectByAlbumId(@BindSqlParam("albumId") long dummy);
}

In the above DAO definitions, method selectAlbum load all albums and for each album, to valorize songs field, uses the DaoSong#selectByAlbumId.

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