Search in sources :

Example 1 with NoPrimaryKeyWithPrimaryKeyObjectRelation

use of io.realm.entities.NoPrimaryKeyWithPrimaryKeyObjectRelation in project realm-java by realm.

the class BulkInsertTests method insertOrUpdate_mixingNoPrimaryKeyAndPrimaryKeyModels.

@Test
public void insertOrUpdate_mixingNoPrimaryKeyAndPrimaryKeyModels() {
    AllTypesPrimaryKey objB_pk = new AllTypesPrimaryKey();
    objB_pk.setColumnLong(7);
    objB_pk.setColumnString("B");
    NoPrimaryKeyWithPrimaryKeyObjectRelation objA_no_pk = new NoPrimaryKeyWithPrimaryKeyObjectRelation();
    objA_no_pk.setColumnRealmObjectPK(objB_pk);
    objA_no_pk.setColumnString("A");
    realm.beginTransaction();
    realm.insert(objA_no_pk);
    realm.commitTransaction();
    RealmResults<NoPrimaryKeyWithPrimaryKeyObjectRelation> all = realm.where(NoPrimaryKeyWithPrimaryKeyObjectRelation.class).findAll();
    assertEquals(1, all.size());
    assertEquals("A", all.get(0).getColumnString());
    assertEquals(8, all.get(0).getColumnInt());
    assertNotNull(all.get(0).getColumnRealmObjectPK());
    assertEquals(7, all.get(0).getColumnRealmObjectPK().getColumnLong());
    assertEquals("B", all.get(0).getColumnRealmObjectPK().getColumnString());
    assertEquals(1, realm.where(AllTypesPrimaryKey.class).findAll().size());
    objA_no_pk.setColumnString("different A");
    // Should insert a new instance
    objA_no_pk.setColumnInt(42);
    // Updates (since it has a PK) now both AllTypesPrimaryKey points to the same objB_pk instance.
    objB_pk.setColumnString("updated B");
    realm.beginTransaction();
    realm.insertOrUpdate(objA_no_pk);
    realm.commitTransaction();
    all = realm.where(NoPrimaryKeyWithPrimaryKeyObjectRelation.class).findAllSorted("columnString");
    assertEquals(2, all.size());
    assertEquals("A", all.get(0).getColumnString());
    assertEquals(8, all.get(0).getColumnInt());
    assertEquals("different A", all.get(1).getColumnString());
    assertEquals(42, all.get(1).getColumnInt());
    assertNotNull(all.get(0).getColumnRealmObjectPK());
    assertNotNull(all.get(1).getColumnRealmObjectPK());
    assertEquals(7, all.get(0).getColumnRealmObjectPK().getColumnLong());
    assertEquals(7, all.get(1).getColumnRealmObjectPK().getColumnLong());
    assertEquals("updated B", all.get(0).getColumnRealmObjectPK().getColumnString());
    assertEquals("updated B", all.get(1).getColumnRealmObjectPK().getColumnString());
    assertEquals(1, realm.where(AllTypesPrimaryKey.class).findAll().size());
}
Also used : NoPrimaryKeyWithPrimaryKeyObjectRelation(io.realm.entities.NoPrimaryKeyWithPrimaryKeyObjectRelation) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Example 2 with NoPrimaryKeyWithPrimaryKeyObjectRelation

use of io.realm.entities.NoPrimaryKeyWithPrimaryKeyObjectRelation in project realm-java by realm.

the class BulkInsertTests method insertOrUpdate_mixingPrimaryAndNoPrimaryKeyList.

@Test
public void insertOrUpdate_mixingPrimaryAndNoPrimaryKeyList() {
    NoPrimaryKeyWithPrimaryKeyObjectRelation objA_no_pk = new NoPrimaryKeyWithPrimaryKeyObjectRelation();
    objA_no_pk.setColumnString("A");
    NoPrimaryKeyWithPrimaryKeyObjectRelation objB_no_pk = new NoPrimaryKeyWithPrimaryKeyObjectRelation();
    objB_no_pk.setColumnString("B");
    AllTypesPrimaryKey objC_pk = new AllTypesPrimaryKey();
    objC_pk.setColumnLong(7);
    objC_pk.setColumnString("C");
    AllTypesPrimaryKey objD_pk = new AllTypesPrimaryKey();
    objD_pk.setColumnLong(7);
    objD_pk.setColumnString("D");
    objA_no_pk.setColumnRealmObjectPK(objC_pk);
    objB_no_pk.setColumnRealmObjectPK(objD_pk);
    ArrayList<NoPrimaryKeyWithPrimaryKeyObjectRelation> objects = new ArrayList<NoPrimaryKeyWithPrimaryKeyObjectRelation>(2);
    objects.add(objA_no_pk);
    objects.add(objB_no_pk);
    realm.beginTransaction();
    realm.insertOrUpdate(objects);
    realm.commitTransaction();
    RealmResults<NoPrimaryKeyWithPrimaryKeyObjectRelation> all = realm.where(NoPrimaryKeyWithPrimaryKeyObjectRelation.class).findAllSorted("columnString", Sort.DESCENDING);
    assertEquals(2, all.size());
    assertEquals("B", all.get(0).getColumnString());
    assertEquals("A", all.get(1).getColumnString());
    assertNotNull(all.get(0).getColumnRealmObjectPK());
    assertNotNull(all.get(1).getColumnRealmObjectPK());
    assertEquals("D", all.get(0).getColumnRealmObjectPK().getColumnString());
    assertEquals("D", all.get(1).getColumnRealmObjectPK().getColumnString());
    assertEquals(1, realm.where(AllTypesPrimaryKey.class).findAll().size());
}
Also used : ArrayList(java.util.ArrayList) NoPrimaryKeyWithPrimaryKeyObjectRelation(io.realm.entities.NoPrimaryKeyWithPrimaryKeyObjectRelation) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Aggregations

AllTypesPrimaryKey (io.realm.entities.AllTypesPrimaryKey)2 NoPrimaryKeyWithPrimaryKeyObjectRelation (io.realm.entities.NoPrimaryKeyWithPrimaryKeyObjectRelation)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1