如何加入ObjectBox

时间:2018-08-11 05:58:59

标签: android objectbox

如何在ObjectBox中加入两个具有一对多关系的课程? 我有两个表,如下所示: @Entity

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.jbdelosreyes.finalmvp"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.google.firebase:firebase-firestore:17.0.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

apply plugin: 'com.google.gms.google-services'

和:

public class Animal {
    @Id(assignable = true)
    public long id;
    private String name;
    private boolean flying;
    private boolean swimming;
    private boolean walking;
    private ToOne<Zoo> zoo;

.../*setters and getters*/

}

如何实现Join操作?

2 个答案:

答案 0 :(得分:1)

2.0.0版开始,您可以执行以下操作:

val builder = box.query().equal(Zoo_.name, "The Big Zoo")
builder.link(Zoo_.animals).equal(Animal_.flying, true)
val flyingAnimals = builder.build().find()

在其上被称为"links" and there is documentation并带有附加示例。

答案 1 :(得分:0)

https://docs.objectbox.io/queries#add-query-conditions-for-related-entities-links

   @Entity
public class Person {
    @Id long id;
    String name;
    ToMany<Address> addresses;
}
@Entity
public class Address {
    @Id long id;
    String street;
    String zip;
}


// get all Person objects named "Elmo"...
QueryBuilder<Person> builder = personBox
        .query().equal(Person_.name, "Elmo");
// ...which have an address on "Sesame Street"
builder.link(Person_.addresses).equal(Address_.street, "Sesame Street");
List<Person> elmosOnSesameStreet = builder.build().find();