private <T> void validatePersistableEntityClass(ClassCacheMgr cacheMgr, CFMappingDef<T> cfMapDef) { // CFMappingDef<? super T> cfSuperDef; if (null == cfMapDef.getEffectiveColFamName()) { throw new HectorObjectMapperException("Class, " + cfMapDef.getRealClass().getName() + ", is missing @" + Table.class.getSimpleName()); } // else if (null != (cfSuperDef = cacheMgr.findBaseClassViaMappings(cfMapDef))) { // throw new HectorObjectMapperException("@" + Table.class.getSimpleName() // + " can only be used once per hierarchy and has been specified in class " // + cfSuperDef.getRealClass().getName() + " and " + cfMapDef.getRealClass().getName() // + " - quitting"); // } }
/** * Load an entity instance. If the ID does not map to a persisted entity, then * null is returned. * * @param <T> The type of entity to load for compile time type checking * @param clazz The type of entity to load for runtime instance creation * @param id ID of the instance to load * @return instance of Entity or null if can't be found */ @Override public <T> T find(Class<T> clazz, Object id) { if (null == clazz) { throw new IllegalArgumentException("clazz cannot be null"); } if (null == id) { throw new IllegalArgumentException("id cannot be null"); } CFMappingDef<T> cfMapDef = cacheMgr.getCfMapDef(clazz, false); if (null == cfMapDef) { throw new HectorObjectMapperException("No class annotated with @" + Entity.class.getSimpleName() + " for type, " + clazz.getName()); } return objMapper.getObject(keyspace, cfMapDef.getEffectiveColFamName(), id); }
private <T> void parseTableAnnotation(ClassCacheMgr cacheMgr, Table anno, CFMappingDef<T> cfMapDef) { CFMappingDef<?> tmpDef; // column family can only be mapped to one class (base class) if (null != (tmpDef = cacheMgr.getCfMapDef(anno.name(), false))) { throw new HectorObjectMapperException( "classes, " + cfMapDef.getEffectiveClass().getName() + " and " + tmpDef.getEffectiveClass().getName() + ", are both mapped to ColumnFamily, " + tmpDef.getEffectiveColFamName() + ". Can only have one Class/ColumnFamily mapping - if multiple classes can be derived from a single ColumnFamily, use @" + Inheritance.class.getSimpleName()); } cfMapDef.setColFamName(anno.name()); }
private void saveObj(Keyspace keyspace, Mutator<byte[]> m, Object obj) { if (null == obj) { throw new IllegalArgumentException("object cannot be null"); } @SuppressWarnings("unchecked") CFMappingDef<Object> cfMapDef = (CFMappingDef<Object>) cacheMgr.getCfMapDef(obj.getClass(), true); byte[] colFamKey = generateColumnFamilyKeyFromPojo(obj, cfMapDef); String colFamName = cfMapDef.getEffectiveColFamName(); // if object contains collection, then must delete everything first - easier // than reading the row and selectively deleting, which is an alternative if // this is too destructive if (cfMapDef.isAnyCollections()) { m.addDeletion(colFamKey, colFamName); } // must create the "add" columns after the delete to insure proper // processing order Collection<HColumn<String, byte[]>> colColl = createColumnSet(obj); for (HColumn<String, byte[]> col : colColl) { if (null == col.getName() || col.getName().isEmpty()) { throw new HectorObjectMapperException( "Column name cannot be null or empty - trying to persist to ColumnFamily, " + colFamName); } m.addInsertion(colFamKey, colFamName, col); } }
cfMapByColFamName.put(cfMapDef.getEffectiveColFamName(), cfMapDef);
@Test public void testInheritanceWithMultiLevels() { ClassCacheMgr cacheMgr = new ClassCacheMgr(); CFMappingDef<Desk> cfMapDef = cacheMgr.initializeCacheForClass(Desk.class); CFMappingDef<Furniture> cfBaseMapDef = cacheMgr.getCfMapDef(Furniture.class, true); assertEquals(7, cfMapDef.getAllProperties().size()); assertNotNull(cfMapDef.getCfSuperMapDef()); assertNotNull(cfMapDef.getCfBaseMapDef()); assertEquals(Desk.class.getSuperclass(), cfMapDef.getCfSuperMapDef().getEffectiveClass()); assertEquals(Desk.class.getSuperclass().getSuperclass(), cfMapDef.getCfSuperMapDef() .getCfSuperMapDef() .getEffectiveClass()); assertEquals(cfBaseMapDef.getEffectiveColFamName(), cfMapDef.getEffectiveColFamName()); assertEquals("type", cfMapDef.getDiscColumn()); assertEquals("table_desk", cfMapDef.getDiscValue()); assertEquals(DiscriminatorType.STRING, cfMapDef.getDiscType()); assertEquals("id", cfMapDef.getKeyDef().getIdPropertyMap().values().iterator().next() .getPropDesc().getName()); }
@Test public void testInheritanceOfEntity() { ClassCacheMgr cacheMgr = new ClassCacheMgr(); CFMappingDef<MyRedTestBean> cfMapDef = cacheMgr.initializeCacheForClass(MyRedTestBean.class); // 13 is valid when custom conversion of enumerations works again // don't like hard coding numbers into JUnits, but took easy way for now assertEquals(14, cfMapDef.getAllProperties().size()); assertNotNull(cfMapDef.getCfBaseMapDef()); assertEquals(MyRedTestBean.class, cfMapDef.getEffectiveClass()); assertEquals("TestBeanColumnFamily", cfMapDef.getEffectiveColFamName()); assertEquals("myType", cfMapDef.getDiscColumn()); assertEquals(DiscriminatorType.STRING, cfMapDef.getDiscType()); assertEquals("baseId", cfMapDef.getKeyDef().getIdPropertyMap().values().iterator().next() .getPropDesc().getName()); // check super class settings assertEquals( MyRedTestBean.class.getSuperclass(), cfMapDef.getCfSuperMapDef().getRealClass()); assertFalse( cfMapDef.getCfSuperMapDef().isColumnSliceRequired()); }