greenrobot / greenDAO

greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases.
http://greenrobot.org/greendao/
12.63k stars 2.89k forks source link

一对多的关系表映射问题 #1099

Closed tomKitty888888 closed 1 year ago

tomKitty888888 commented 1 year ago

@Entity public class 我 {

@Id(autoincrement = true)
Long id;

@ToMany(referencedJoinProperty = "masterId")
private 钱 钱List;

}

@Entity public class 钱 {

@Id(autoincrement = true)
Long id;

Long masterId;

} 注解都有,查询不到list数据

AncientTree commented 7 months ago

我也遇到这个问题,toMany 的list从数据库读出来是null,请问一下你是如何解决的?

tomKitty888888 commented 7 months ago

在两个关系表所对应的实体类中需要进行1对多的关联配置,如果已经配置好了,那么在检查在取多数据时的方法:不是直接get类中对应的list,是get关联配置的那个方法才能获取到数据,希望对你有用。

@. @.

 

------------------ 原始邮件 ------------------ 发件人: "greenrobot/greenDAO" @.>; 发送时间: 2023年11月4日(星期六) 晚上7:00 @.>; @.**@.>;"State @.***>; 主题: Re: [greenrobot/greenDAO] 一对多的关系表映射问题 (Issue #1099)

我也遇到这个问题,toMany 的list从数据库读出来是null,请问一下你是如何解决的?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you modified the open/close state.Message ID: @.***>

AncientTree commented 7 months ago

谢谢,已经解决啦,找到问题所在了。和你说的这个情况很像,我是通过属性访问modelOne.modelTwoLIst,每次都是null,这是我用法错误。正确用法是modelOne.getModelTwoList() ,GreenDao在首次Build的时候已经给ModelOne中所有标记有@ToMany的属性自动生成了getter,这个自动生成的getter首次被调用时会自动读取数据库来加载填充modelTwoLIst。谢谢你的指导,也为后来朋友提供个参考。