gpt4 book ai didi

Android 房间持久性库

转载 作者:行者123 更新时间:2023-11-30 00:26:57 27 4
gpt4 key购买 nike

我是 Room 的新手,@Relation 对我来说不是很清楚。如果我理解正确,我有实体,例如(RSS)ChannelEntity, channel 具有名为 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 接口(interface),我可以像这样获取 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,我可以获得 Channel 对象,其中包含具有相应链接 (id) 的 Items 列表。是吗?

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

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

删除

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

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

最佳答案

我按照@CommonsWare 的建议更新了我的类(class)。现在我有了带有@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(我将嵌入式类称为实体,即使它们不是实体,但我有 View 的模型类,我不想以后混淆它们)。

我这样映射关系:

// 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

当我想获得一个包含项目列表的 channel 时,我是这样得到的:

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

关于Android 房间持久性库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45172455/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com