【问题标题】:Android Room Persistance LibraryAndroid 房间持久性库
【发布时间】:2017-07-19 17:14:11
【问题描述】:

我是 Room 新手,@Relation 对我来说不是很清楚。 如果我理解正确,我有实体,例如(RSS)ChannelEntity,并且频道有名为 ItemEntity 的项目。 这些是带有 @Entity 注释的类。 我还有一个 POJO 来“连接”我的实体。我的意思是我必须像这样写一个 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) 的项目列表的 Channel 对象。对吗?

我的另一个问题是关于其余的 CRUD。例如。如果我想保存一个新频道,我可以将此语句添加到我的 ChannelDao 吗?

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

删除

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

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

【问题讨论】:

  • 您是在询问还是尝试过但不起作用? Room Persistence Library 是新的(和 alpha 版),所以没有很多有经验的 android 用户有时间使用它(考虑到它不太可能在生产中使用尚未),所以最好拿出有具体错误,一般样本;听起来您只是想获得验证,但没有人有时间对此进行测试。因此,继续尝试,然后在发生错误时报告错误。并发布所有相关课程,而不是仅发布其中的 1/2。
  • "我可以将此语句添加到我的 ChannelDao 吗?" - AFAIK,不。 @Relation 背后的想法,据我所知,是用它来填充视图模型。
  • 我还没有使用它,起初我想了解如何使用包含列表或其他对象的 Room 对象从 db 中持久化和读取它。如果我使用@ForeignKey,我可以在一个查询中取回带有列表的对象。
  • @CommonsWare 所以你说我必须为这些实体写 daos 并且我可以使用 Relation annotation 来获取 Channel 对象?
  • 我是说 AFAIK @Relation 是一个只读结构。当您想保存东西时,AFAIK,您使用的是实体(或 @Query 中的自定义 SQL),而不是带有 @Relation 的 POJO。

标签: android android-room


【解决方案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

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

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:

@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();
}

【讨论】:

    猜你喜欢
    • 2018-01-22
    • 2018-05-27
    • 1970-01-01
    • 2020-03-19
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多