在ROOM中联接表时,表中的字段名称相同 [英] The field's same name issue in the tables, when join tables in ROOM
问题描述
我正在为我的项目使用ROOM.但是我无法在表之间进行连接,因为表具有相同的名称字段.
例如,我的项目有树表"word"和"收藏夹"和书签",具有相同的名称字段.
for example, my project has tree tables, "word" & "favorite" & "bookmark", which has a same name fields.
1个字
@Entity(tableName = "word")
public class Word {
@NonNull
@PrimaryKey(autoGenerate = true)
private int id;
@NonNull
private String title;
private String mean;
private String pronunciation;
// Getter and Setter
}
2-最喜欢的
@Entity(tableName = "favorite",
foreignKeys = @ForeignKey(
entity = Word.class,
parentColumns = "id",
childColumns = "word_id",
onDelete = CASCADE,
onUpdate = CASCADE))
public class Favorite {
@NonNull
@PrimaryKey
private int word_id;
private long time;
// Getter and Setter
}
3-书签
@Entity(tableName = "bookmark",
primaryKeys = {"word_id", "category_id"},
foreignKeys = {
@ForeignKey(entity = Word.class,
parentColumns = "id",
childColumns = "word_id",
onDelete = CASCADE,
onUpdate = CASCADE)})
public class Bookmark {
@NonNull
private int word_id;
private long time;
private int color;
// Getter and Setter
}
要在这三个之间创建连接,我定义了一个新类型,称为"WordAndFavoriteAndBookmark",并使用了"@Embedded"(如下所示). 为了解决相同的现场问题,我使用 prefix 作为 Thomas Fischer 响应
To create a join between the three, I defined a new type called "WordAndFavoriteAndBookmark", and used "@Embedded" (as shown below). and to fix the same field problem, I used prefix as a Thomas Fischer response
4- WordAndFavoriteAndBookmark
public class WordAndFavoriteAndBookmark {
@Embedded
private Word word;
@Embedded(prefix = "favorite_")
private Favorite favorite;
@Embedded(prefix = "bookmark_")
private Bookmark bookmark;
//Getter and Setter
public Word getWord() { return word; }
public void setWord(Word word) {this.word = word;}
public Favorite getFavorite() { return favorite; }
public void setFavorite(Favorite favorite) { this.favorite = favorite;}
public Bookmark getBookmark() { return bookmark; }
public void setBookmark(Bookmark bookmark) { this.bookmark = bookmark; }
}
要在这些表之间创建联接,我定义了一个新的 @Dao .
To create a join between these tables I have defined a new @Dao.
@Dao
public interface WordAndFavoriteAndBookmarkDao {
@Query("SELECT word.*, favorite.time FROM word LEFT JOIN favorite " +
"ON word.id = favorite.word_id")
LiveData<List<WordAndFavoriteAndBookmark>> getAllWordsByFavoritesForLanguage();
}
但是,再次使用查询后,在我的视图(活动性或片段)中遇到了以下代码的错误:
But again, after using the query, I encounter the error for the following code in my view(Activity or Fragment):
mWordAndFavoriteAndBookmark.getFavorite().getTime();
我认为这是由于使用了 Perfix ,但我不知道解决该问题的方法
I think this is due to the use of Perfix, but I do not know the solution to this problem
由 Thomas Fisher 回答, 一切都好.但是在查询中使用计数"或总和"时,我在读取这些值时遇到了问题,而且我不知道如何读取它们.
EDITED: by Thomas Fisher answer, everything is fine. But when used "Count" or "Sum" in my query, I have problem for reading these values and I don't know how to read them.
推荐答案
为解决"Count"或"Sum"字段问题,我使用了新的复合数据类. 如下:
To solve the "Count" or "Sum" field problem, I used the new compound data class. As below:
public static class CategoryAndEssentials {
@Embedded
private BookmarkCategory bookmarkCategory;
private int wordCount;
public BookmarkCategory getBookmarkCategory() { return bookmarkCategory; }
public void setBookmarkCategory(BookmarkCategory bookmarkCategory) { this.bookmarkCategory = bookmarkCategory;}
public int getWordCount() { return wordCount; }
public void setWordCount(int wordCount) { this.wordCount = wordCount; }
}
在查询定义的Dao类中,我引用了该字段:
And in the query definition, in Dao class, I referred to this field:
@Query("SELECT " +
"bookmarkCategory.*, " +
"COUNT(bookmark.category_id) AS wordCount, " +
"FROM bookmarkCategory LEFT JOIN bookmark " +
"ON bookmarkCategory.id = bookmark.category_id " +
"GROUP BY bookmarkCategory.id ")
LiveData<List<EntityClassForJoinTables.CategoryAndEssentials>> getAllCategoriesAndEssentials();
请注意,查询中的别名列必须与复合数据类中定义的变量相同
Note that the alias column in query must have a same as the variable defined in the compound data class
这篇关于在ROOM中联接表时,表中的字段名称相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!