2016-01-13 82 views
1

我有兩個實體,我試圖聯繫,雖然我正在從服務器正確檢索關係的實體,我不知道如果我做得正確。Android Greendao 1:N的關係

我有兩個實體:AccountSong

當一個Account可以有很多Song和一個Song只能屬於一個Account

在我這一代的文件,我也做了以下內容:

Entity account = schema.addEntity("Account"); 
account.addLongProperty("id").primaryKey().notNull().getProperty(); 
Property accountId = account.addLongProperty("accountId").getProperty(); 
account.addStringProperty("name"); 
account.addStringProperty("comments"); 

Entity song = schema.addEntity("Song"); 
song.addLongProperty("id").primaryKey().notNull().getProperty(); 
Property songId = song.addLongProperty("songId").getProperty(); 
song.addStringProperty("name"); 
song.addStringProperty("artist"); 

ToMany accountToSongs = account.addToMany(song, accountId); 
accountToSongs.setName("songs"); 
song.addToOne(account, songId); 

當我對服務器進行HTTP調用時,我可以做一個account.getSongs()並看到r在那裏,我知道這些並不會持久,當你在sqlite中執行insert

現在我需要訪問Account模型的給定id的所有歌曲,但我不太確定我是否錯過了一個步驟或者是否生成了錯誤的文件。我是否應該手動將songs設置爲account?我在文件生成中的屬性是否有誤?

回答

0

我設法通過改變我的文件生成項目的構建來解決這個問題。

我做了以下代替:

Entity account = schema.addEntity("Account"); 
account.addLongProperty("id").primaryKey().notNull().getProperty(); 
Property accountId = account.addLongProperty("accountId").getProperty(); 
account.addStringProperty("name"); 
account.addStringProperty("comments"); 

Entity song = schema.addEntity("Song"); 
song.addLongProperty("id").primaryKey().notNull().getProperty(); 
Property accountId = song.addLongProperty("accountId").getProperty(); 
song.addStringProperty("name"); 
song.addStringProperty("artist"); 

ToMany songToAccounts = account.addToMany(song, accountId); 
songToAccounts.setName("songs"); 
song.addToOne(account, accountId); 

我幾乎反其道而行之創造我的外國實體的屬性時。我對文字有點困惑,但一般來說,這種方式很有效。只要確保你知道在持久化父元素時外部實體不會被持久化。