Android Room Persistance Library
问题描述:
我是新来的房间,而@Relation对我来说还不清楚。 如果我理解正确,我有实体例如(RSS)ChannelEntity,并且频道具有名为ItemEntity的项目。 这些是带有@Entity注解的类。 我也有一个POJO来“连接”我的实体。我的意思是我写这样一个POJO:Android Room Persistance Library
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接口在哪儿能买到通道(未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的问题。例如。如果我想保存一个新频道,我可以将此声明添加到我的ChannelDao中吗?
@Insert(onConflict = OnConflictStrategy.REPLACE)
void createChannels(Channel... channels);
删除
@Delete
void deleteChannels(Channel... channels);
等。那么它会从传递的Channel对象创建并删除ChannelEntities和ItemEntities?
答
我更新了我的课程,作为@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;
}
我写的daos所有的班级有@Entity批注(我称为嵌入式类实体甚至他们不是,但我有模型类的意见,我不想混淆他们以后)。
我这样映射关系:
// 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
}
这是海峡道:
@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();
}
你问或者你有没有试过,这是行不通的?房间持久性图书馆是新的(和阿尔法),所以没有很多有经验的安卓用户有时间玩它(因为它不太可能用于生产* *),所以最好提出具体的错误,样品一般来说;这听起来像你只是想获得验证,但没有人会有时间来测试。因此,请继续尝试,然后在发生错误时报告错误。并发布你所有的相关课程,也不只是其中的1/2。 –
“我可以将此声明添加到我的ChannelDao吗?” - AFAIK,没有。 “@关系”背后的想法,我可以说,就是用它来填充视图模型。 – CommonsWare
我还没有使用它,起初我想知道如何使用包含列表或其他对象的房间对象从数据库持久化和读取它。如果我使用@ForeignKey,我可以在一个查询中找回列表中的对象。 – user3057944