objectbox / objectbox-java

Android Database - first and fast, lightweight on-device vector database
https://objectbox.io
Apache License 2.0
4.38k stars 302 forks source link

How to use multiple entity ids in a entity? #853

Closed Divya21 closed 4 years ago

Divya21 commented 4 years ago

I have 4 entities(4 tables) and I want to use multiple entities ids in another entity. How to inherit it. Please help!

@Entity 
data class A ( 
@Id var idA: Long = 0,
var nameA: String? = null
) 

@Entity 
data class B ( 
@Id var idB: Long = 0,
var nameB: String? = null
) 

@Entity 
data class C ( 
@Id var idC: Long = 0,
var nameC: String? = null
) 

@Entity
data class MemberTable(
    @Id
    var memberId: Long =0,

//I want to to do something like this...How to achieve this?
var idsA: List<A.idA>? = null,
var idsB: List<B.idB>? = null,
var idsC: List<C.idC>? = null,

)
greenrobot-team commented 4 years ago

If you only want to store IDs you have to do it yourself. With ObjectBox, there are relations. In this case adding ToMany<A>, ToMany<B>, etc. to MemberTable would work. https://docs.objectbox.io/relations

Divya21 commented 4 years ago

If you only want to store IDs you have to do it yourself. With ObjectBox, there are relations. In this case adding ToMany, ToMany, etc. to MemberTable would work. https://docs.objectbox.io/relations

I have to use List of Int for this, I have seen it has been added to features in 2016(link is below). So is there any way? https://github.com/objectbox/objectbox-java/issues/7#issue-202969381

greenrobot-team commented 4 years ago

You would have to write a custom converter for this. https://docs.objectbox.io/advanced/custom-types

Alternatively, use ToMany<A> anyhow. But get the list of IDs of the related entities using Box.getRelationIds(MemberTable_.toManyOfA, memberTable.memberId). https://github.com/objectbox/objectbox-java/blob/220fc35ef296b9957a29207596abf8c1313dfcb5/objectbox-java/src/main/java/io/objectbox/Box.java#L657

Divya21 commented 4 years ago

You would have to write a custom converter for this. https://docs.objectbox.io/advanced/custom-types

Alternatively, use ToMany<A> anyhow. But get the list of IDs of the related entities using Box.getRelationIds(MemberTable_.toManyOfA, memberTable.memberId). https://github.com/objectbox/objectbox-java/blob/220fc35ef296b9957a29207596abf8c1313dfcb5/objectbox-java/src/main/java/io/objectbox/Box.java#L657

Thanks a lot!