首页 文章

Android Room Persistance Library

提问于
浏览
1

我是Room的新手,而@Relation对我来说并不清楚 . 如果我理解正确,我有实体,例如(RSS)ChannelEntity,channel有名为ItemEntity的项目 . 这些是带有@Entity注释的类 . 我也有一个POJO来“连接”我的entites . 我的意思是我要像这样写一个POJO:

public class Channel { 

   @Embedded
   private ChannelEntity channel;

// Link is the primary key in ChannelyEntity
   @Relation (parentColumn = "link", entityColumn = "channel_link")
   private ArrayList<ItemEntity> items;

// Getters and Setters are here
}

我要编写一个dao界面,我可以像这样得到Channel(不是ChannelEntity):

public interface ChannelDao {

    @Query("SELECT * FROM channels WHERE link = :link LIMIT 1")
    Channel getChannelById(String link);

    @Query("SELECT * FROM channels")
    ArrayList<Channel> getAllChannels();
}

通过这些实体,DAO和POJO,我可以获得包含具有相应链接(id)的Items列表的Channel对象 . 是对的吗?

我的另一个问题是关于其余的CRUD . 例如 . 如果我想保存一个新 Channels ,我可以将此声明添加到我的ChannelDao吗?

@Insert(onConflict = OnConflictStrategy.REPLACE)
void createChannels(Channel... channels);

删除

@Delete
void deleteChannels(Channel... channels);

等等 . 那么它会从传递的Channel对象中创建和删除ChannelEntities和ItemEntities吗?

1 回答

  • 1

    我更新了我的课程,因为@CommonsWare提供了建议 . 现在我有@Embedded对象的实体类:

    @Entity (tableName = "channels")
    public class ChannelEntity {
        // Required channel elements
        // The name of the channel. It's how people refer to your service.
        private String title;
        // The URL of the HTML website corresponding to the channel
        @PrimaryKey
        private String link;
        //other fileds
        @Embedded
        private TextInputEntity textInputEntity;
        @Embedded
        private ImageEntity imageEntity;
        //getters and setters
    }
    

    其中一个嵌入式类:

    // Specifies a text input box displayed with the channel.
    // Embedded in ChannelEntity
    public class TextInputEntity {
        // Required elements
        // The label of the Submit button in the text input area.
        private String title;
        // Explains the text input aera.
        private String description;
        // The name of the text object int hte text input area.
        private String name;
        // The URL of the CGI script that processes the text input request
        private String link;
        @ColumnInfo (name = "channel_link")
        private String channelLink;
    }
    

    我已经为所有类编写了带有@Entity注释的daos(我称之为嵌入式类实体,即使它们不是,但我有视图的模型类,我不想在以后混淆它们) .

    我映射了这样的关系:

    // All items are optional, but at least one of title or description must be presented.
    @Entity (tableName = "items"
            , foreignKeys = @ForeignKey (entity = Channel.class
            , parentColumns = "link"
            , childColumns = "channel_link"))
    public class ItemEntity {
        @PrimaryKey (autoGenerate = true)
        private int id;
        // Title of the item
        private String title;
        // Other fileds
        @ColumnInfo (name = "channel_link")
        private String channelLink;
        // Getters and setters
    

    当我想获得一个包含项目列表的 Channels 时,我会这样:

    public class Channel {
        @Embedded
        private ChannelEntity channel;
        @Relation (parentColumn = "link", entityColumn = "channel_link")
        private ArrayList<ItemEntity> items;
        @Relation (parentColumn = "link", entityColumn = "channel_link")
        private ArrayList<SkipDayEntity> skipDays;
        @Relation (parentColumn = "link", entityColumn = "channel_link")
        private ArrayList<SkipHourEntity> skipHours;
    //Setters and getters
    }
    

    这是Channel的道:

    @Dao
    public interface ChannelDao {
        @Insert (onConflict = OnConflictStrategy.REPLACE)
        void insertChannel(ChannelEntity channel);
    
        @Update
        void updateChannel(ChannelEntity channel);
    
        @Delete
        void deleteChannel(ChannelEntity channel);
    
        @Query ("SELECT * FROM channles WHERE link = :link LIMIT 1")
        Channel getChannelByLink(String link);
    
        @Query ("SELECT * FROM channels")
        LiveData<ArrayList<Channel>> getAllChannels();
    }
    

相关问题