@Override protected ToManyTargetEntity createEntity(Long key) { ToManyTargetEntity entity = new ToManyTargetEntity(); entity.setId(key); return entity; }
@Override public Long getKey(ToManyTargetEntity entity) { if(entity != null) { return entity.getId(); } else { return null; } }
@Override public void readEntity(Cursor cursor, ToManyTargetEntity entity, int offset) { entity.setToManyId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); entity.setToManyIdDesc(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1)); entity.setId(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2)); entity.setTargetJoinProperty(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3)); }
private ToManyTargetEntity[] insertTargetEntities(Long toManyId, int count, String joinProperty) { ToManyTargetEntity[] targetEntities = new ToManyTargetEntity[count]; for (int i = 0; i < count; i++) { ToManyTargetEntity target = new ToManyTargetEntity(); target.setToManyId(toManyId); target.setToManyIdDesc(toManyId); target.setTargetJoinProperty(joinProperty); targetEntities[i] = target; } toManyTargetEntityDao.insertInTx(targetEntities); return targetEntities; }
@Override protected final void bindValues(DatabaseStatement stmt, ToManyTargetEntity entity) { stmt.clearBindings(); Long toManyId = entity.getToManyId(); if (toManyId != null) { stmt.bindLong(1, toManyId); } Long toManyIdDesc = entity.getToManyIdDesc(); if (toManyIdDesc != null) { stmt.bindLong(2, toManyIdDesc); } Long id = entity.getId(); if (id != null) { stmt.bindLong(3, id); } String targetJoinProperty = entity.getTargetJoinProperty(); if (targetJoinProperty != null) { stmt.bindString(4, targetJoinProperty); } }
public void testTwoJoinProperty() { ToManyEntity entity = new ToManyEntity(1L); entity.setSourceJoinProperty("JOIN ME"); daoSession.insert(entity); insertTargetEntities(1L, 3, "JOIN ME"); ToManyEntity testEntity = toManyEntityDao.load(1L); List<ToManyTargetEntity> targetEntities = testEntity.getToManyJoinTwo(); assertEquals(3, targetEntities.size()); ToManyTargetEntity middleEntity = targetEntities.get(1); middleEntity.setTargetJoinProperty("DON'T JOIN ME"); toManyTargetEntityDao.update(middleEntity); testEntity.resetToManyJoinTwo(); targetEntities = testEntity.getToManyJoinTwo(); assertEquals(2, targetEntities.size()); assertFalse(middleEntity.getId() == targetEntities.get(0).getId()); assertFalse(middleEntity.getId() == targetEntities.get(1).getId()); }
public void testChangeResetToMany() { prepareToMany(1, 3); ToManyEntity testEntity = toManyEntityDao.load(1L); List<ToManyTargetEntity> resolvedToMany1 = testEntity.getToManyTargetEntityList(); testEntity.resetToManyTargetEntityList(); ToManyTargetEntity newEntity = new ToManyTargetEntity(); newEntity.setToManyId(1L); daoSession.insert(newEntity); List<ToManyTargetEntity> resolvedToMany2 = testEntity.getToManyTargetEntityList(); assertNotSame(resolvedToMany1, resolvedToMany2); assertEquals(resolvedToMany1.size() + 1, resolvedToMany2.size()); testEntity.resetToManyTargetEntityList(); toManyTargetEntityDao.deleteAll(); List<ToManyTargetEntity> resolvedToMany3 = testEntity.getToManyTargetEntityList(); assertEquals(0, resolvedToMany3.size()); }
@Override protected final Long updateKeyAfterInsert(ToManyTargetEntity entity, long rowId) { entity.setId(rowId); return rowId; }
@Override public ToManyTargetEntity readEntity(Cursor cursor, int offset) { ToManyTargetEntity entity = new ToManyTargetEntity( // cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // toManyId cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // toManyIdDesc cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2), // id cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3) // targetJoinProperty ); return entity; }
@Override protected final void bindValues(SQLiteStatement stmt, ToManyTargetEntity entity) { stmt.clearBindings(); Long toManyId = entity.getToManyId(); if (toManyId != null) { stmt.bindLong(1, toManyId); } Long toManyIdDesc = entity.getToManyIdDesc(); if (toManyIdDesc != null) { stmt.bindLong(2, toManyIdDesc); } Long id = entity.getId(); if (id != null) { stmt.bindLong(3, id); } String targetJoinProperty = entity.getTargetJoinProperty(); if (targetJoinProperty != null) { stmt.bindString(4, targetJoinProperty); } }
public void testJoinProperty() { ToManyEntity entity = new ToManyEntity(1L); entity.setSourceJoinProperty("JOIN ME"); daoSession.insert(entity); insertTargetEntities(null, 3, "JOIN ME"); ToManyEntity testEntity = toManyEntityDao.load(1L); List<ToManyTargetEntity> targetEntities = testEntity.getToManyByJoinProperty(); assertEquals(3, targetEntities.size()); ToManyTargetEntity middleEntity = targetEntities.get(1); middleEntity.setTargetJoinProperty("DON'T JOIN ME"); toManyTargetEntityDao.update(middleEntity); testEntity.resetToManyByJoinProperty(); targetEntities = testEntity.getToManyByJoinProperty(); assertEquals(2, targetEntities.size()); assertFalse(middleEntity.getId() == targetEntities.get(0).getId()); assertFalse(middleEntity.getId() == targetEntities.get(1).getId()); }
@Override public boolean hasKey(ToManyTargetEntity entity) { return entity.getId() != null; }
private void assertSameEntities(ToManyTargetEntity[] targetEntities, List<ToManyTargetEntity> resolvedToMany) { int count = targetEntities.length; assertEquals(count, resolvedToMany.size()); Map<Long, ToManyTargetEntity> resolvedMap = new HashMap<>(); for (ToManyTargetEntity resolvedEntity : resolvedToMany) { resolvedMap.put(resolvedEntity.getId(), resolvedEntity); } for (int i = 0; i < count; i++) { long entityId = targetEntities[i].getId(); assertTrue("ID=" + entityId, resolvedMap.containsKey(entityId)); assertSame(targetEntities[i], resolvedMap.get(entityId)); } }
public void testToManyOrder() { prepareToMany(1, 3); ToManyEntity testEntity = toManyEntityDao.load(1L); List<ToManyTargetEntity> resolvedToManyAsc = testEntity.getToManyTargetEntityList(); List<ToManyTargetEntity> resolvedToManyDesc = testEntity.getToManyDescList(); assertNotSame(resolvedToManyAsc, resolvedToManyDesc); assertEquals(resolvedToManyAsc.get(0).getId(), resolvedToManyDesc.get(2).getId()); assertSame(resolvedToManyAsc.get(0), resolvedToManyDesc.get(2)); assertSame(resolvedToManyAsc.get(1), resolvedToManyDesc.get(1)); assertSame(resolvedToManyAsc.get(2), resolvedToManyDesc.get(0)); }