0% found this document useful (0 votes)
36 views3 pages

Room Dao

The document defines entities and data access objects for a Room database to store lists and list items. ListItemEntity and GListEntity classes are defined as entities with attributes and relationships. ListItemEntityDao and GListEntityDao interfaces define data access methods. ListDatabase class defines the database with the entities. Code shows using the DAOs to insert and query data.

Uploaded by

Refat Nafiu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views3 pages

Room Dao

The document defines entities and data access objects for a Room database to store lists and list items. ListItemEntity and GListEntity classes are defined as entities with attributes and relationships. ListItemEntityDao and GListEntityDao interfaces define data access methods. ListDatabase class defines the database with the entities. Code shows using the DAOs to insert and query data.

Uploaded by

Refat Nafiu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import android.arch.persistence.room.

Entity;
import android.arch.persistence.room.PrimaryKey;

@Entity
public class GListEntity {

@PrimaryKey public final String listNameKey;


public final String listName;
public final long date;

public GListEntity(String listNameKey, String listName,long date) {

}
}

-----------------------------------------------------------------

import android.arch.persistence.room.Entity;
import android.arch.persistence.room.ForeignKey;
import android.arch.persistence.room.PrimaryKey;

import static android.arch.persistence.room.ForeignKey.CASCADE;

@Entity(foreignKeys = @ForeignKey(entity = GListEntity.class,


parentColumns = "listNameKey",
childColumns = "listNameKey",
onDelete = CASCADE))
public class ListItemEntity {
@PrimaryKey(autoGenerate = true) public final int id;
public final String listNameKey;
public final String itemName;
public final String itemCatalog;
public final boolean purchased ;
public final int quantity ;
public final int listPosition ;
public final int price;

public ListItemEntity() {

}
}

================================================================================
@Dao
public interface ListItemEntityDao {

@Insert
void insert(ListItemEntity repo);

@Update
void update(ListItemEntity... repos);

@Delete
void delete(ListItemEntity... repos);

@Query("SELECT * FROM repo")


List<ListItemEntity> getAllRepos();
@Query("SELECT * FROM repo WHERE userId=:userId")
List<Repo> findRepositoriesForUser(final int userId);
}

===================================================================================
=
@Dao
public interface GListEntityDao {

@Insert
void insert(GListEntity... glistentity);

@Update
void update(GListEntity... glistentity);

@Delete
void delete(GListEntity... glistentity);
}

===================================================================================
===

@Database(entities = { ListItemEntity.class, GListEntity.class },


version = 1)
public abstract class ListDatabase extends RoomDatabase {
...
public abstract ListItemEntityDao getListItemDao();
public abstract GListEntityDao getGListDao();
}

===================================================================================
===

ListItemEntityDao repoDao = RepoDatabase


.getInstance(context)
.getListItemDao();

GListEntityDao userDao = RepoDatabase


.getInstance(context)
.getGListDao();

userDao.insert(new User(1,
"Jake Wharton",
"https://avatars0.githubusercontent.com/u/66577"));

repoDao.insert(new Repo(1,
"square/retrofit",
"https://github.com/square/retrofit",
1));

List<Repo> repositoriesForUser = repoDao.


findRepositoriesForUser(1);

===============================================================================

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy