@Test(expected = HectorObjectMapperException.class) public void testDupeEntityColumnFamilyMapping() { ClassCacheMgr cacheMgr = new ClassCacheMgr(); cacheMgr.initializeCacheForClass(MyDupeCF1.class); cacheMgr.initializeCacheForClass(MyDupeCF2.class); }
@Test(expected = HectorObjectMapperException.class) public void testGetColFamMapDefByClassAnonymousSubclassNotOK() { ClassCacheMgr cacheMgr = new ClassCacheMgr(); cacheMgr.initializeCacheForClass(MyTestBean.class); assertTrue(tmplMap.get(2L).getClass().isAnonymousClass()); cacheMgr.getCfMapDef(tmplMap.get(2L).getClass(), true); }
private <T> void checkMappingAndSetDefaults(CFMappingDef<T> cfMapDef) { inheritanceParVal.validateAndSetDefaults(this, cfMapDef); tableParVal.validateAndSetDefaults(this, cfMapDef); idClassParVal.validateAndSetDefaults(this, cfMapDef); anonymousParVal.validateAndSetDefaults(this, cfMapDef); // must do this after tabeParVal validate checkForPojoPrimaryKey(cfMapDef); // checkForAnonymousHandler(cfMapDef); generateColumnSliceIfNeeded(cfMapDef); }
/** * Load an entity instance given the raw column slice. This is a stop gap * solution for instanting objects using entity manager while iterating over * rows. * * @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 * @param colSlice Raw row slice as returned from Hector API, of the type * <code>ColumnSlice<String, byte[]></code> * @return Completely instantiated persisted object */ public <T> T find(Class<T> clazz, Object id, ColumnSlice<String, byte[]> colSlice) { 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()); } T obj = objMapper.createObject(cfMapDef, id, colSlice); return obj; }
private <T> CFMappingDef<T> initializeColumnFamilyMapDef(Class<T> realClass) { CFMappingDef<T> cfMapDef = getCfMapDef(realClass, false); if (null != cfMapDef) { return cfMapDef; cfSuperMapDef = initializeCacheForClass(effectiveType.getSuperclass()); cfMapDef.setCfSuperMapDef(cfSuperMapDef); } catch (HomMissingEntityAnnotationException e) {
Class<T> theType = cfMapDef.getEffectiveClass(); Map<String, PropertyDescriptor> pdMap = getFieldPropertyDescriptorMap(theType); if (pdMap.isEmpty() && !cfMapDef.isPersistableDerivedEntity()) { throw new HectorObjectMapperException("could not find any properties annotated with @" processIdAnnotation(f, (Id) anno, cfMapDef, pdMap); } else if (anno instanceof me.prettyprint.hom.annotations.Id) { processIdCustomAnnotation(f, (me.prettyprint.hom.annotations.Id) anno, cfMapDef, pdMap);
CFMappingDef<T> cfMapDef = initializeColumnFamilyMapDef(clazz); try { initializePropertiesMapDef(cfMapDef); } catch (IntrospectionException e) { throw new HectorObjectMapperException(e); checkMappingAndSetDefaults(cfMapDef);
public EntityManagerImpl(Keyspace keyspace, EntityManagerFactory emf, Class<?>... classes) { this(keyspace, null, null, null); for (Class<?> clazz : classes) { cacheMgr.initializeCacheForClass(clazz); } this.emf = emf; }
public EntityManagerImpl(Keyspace keyspace, String[] classpathPrefix, ClassCacheMgr cacheMgr, HectorObjectMapper objMapper, AnnotationScanner scanner) { this.keyspace = keyspace; if (null != cacheMgr) { this.cacheMgr = cacheMgr; } else { this.cacheMgr = new ClassCacheMgr(); } if (null != objMapper) { this.objMapper = objMapper; } else { this.objMapper = new HectorObjectMapper(this.cacheMgr); } initialize(scanner, classpathPrefix); }
private <T> void findAndSetBaseClassViaMappings(ClassCacheMgr cacheMgr, CFMappingDef<T> cfMapDef) { CFMappingDef<? super T> cfBaseMapDef = cacheMgr.findBaseClassViaMappings(cfMapDef); if (null == cfBaseMapDef) { throw new HectorObjectMapperException(cfMapDef.getRealClass() + " is a derived class entity but @" + Table.class.getSimpleName() + " is not specified in its super classes - quitting"); } cfMapDef.setCfBaseMapDef(cfBaseMapDef); } }
@Override public <T> void validateAndSetDefaults(ClassCacheMgr cacheMgr, CFMappingDef<T> cfMapDef) { KeyDefinition keyDef = cfMapDef.getKeyDef(); if ( null == keyDef.getPkClazz() ) { return; } Map<String, PropertyDescriptor> pdMap; try { pdMap = cacheMgr.getFieldPropertyDescriptorMap(keyDef.getPkClazz()); } catch (IntrospectionException e) { throw new HectorObjectMapperException("exception while introspecting class, " + keyDef.getPkClazz().getName(), e); } if ( keyDef.getIdPropertyMap().size() != pdMap.size() ) { throw new HectorObjectMapperException("Each field in the primary key class, " + keyDef.getPkClazz().getName() + ", must have a corresponding property in the entity, " + cfMapDef.getRealClass().getName() + ", annotated with @" + Id.class.getSimpleName() ); } for ( String idFieldName : pdMap.keySet() ) { if ( !keyDef.getIdPropertyMap().containsKey(idFieldName)) { throw new HectorObjectMapperException("Each field in the primary key class, " + keyDef.getPkClazz().getName() + ", must have a corresponding property in the entity, " + cfMapDef.getRealClass().getName() + ", annotated with @" + Id.class.getSimpleName() + " : missing ID field, " + idFieldName); } } }
/** * 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 void initializeClasspath(AnnotationScanner scanner, String classpathPrefix) { Set<Class<?>> classSet = scanner.scan(classpathPrefix, Entity.class); for (Class<?> clazz : classSet) { cacheMgr.initializeCacheForClass(clazz); } }
@Test public void testNotSerializable() { ClassCacheMgr cacheMgr = new ClassCacheMgr(); IdClassParserValidator parVal = new IdClassParserValidator(); CFMappingDef<MyTestBean> cfMapDef = new CFMappingDef<MyTestBean>(MyTestBean.class); IdClass anno = new IdClass() { @Override public Class<? extends Annotation> annotationType() { return IdClass.class; } @SuppressWarnings("rawtypes") @Override public Class value() { return MyCompositePK.class; } }; parVal.parse(cacheMgr, anno, cfMapDef); } }
@Override public <T> void validateAndSetDefaults(ClassCacheMgr cacheMgr, CFMappingDef<T> cfMapDef) { if (cfMapDef.isBaseEntity()) { validateBaseClassInheritance(cfMapDef); } else if (cfMapDef.isPersistableDerivedEntity()) { validateDerivedClassInheritance(cfMapDef); } else if (!cfMapDef.isNonPersistableDerivedEntity()) { if (null != cacheMgr.findBaseClassViaMappings(cfMapDef)) throw new HectorObjectMapperException("@" + Inheritance.class.getSimpleName() + " found in class hierarchy, but no @" + DiscriminatorValue.class.getSimpleName() + " - quitting"); } }
@Test(expected = HectorObjectMapperException.class) public void testParsingComplexIdFieldMissing() { ClassCacheMgr cacheMgr = new ClassCacheMgr(); cacheMgr.initializeCacheForClass(MyComplexEntityMissingIdField.class); }
@Test(expected = HectorObjectMapperException.class) public void testGetColFamMapDefByClassSubclassNotOK() { ClassCacheMgr cacheMgr = new ClassCacheMgr(); cacheMgr.initializeCacheForClass(MyTestBean.class); NewBean obj = new NewBean(); assertFalse(obj.getClass().isAnonymousClass()); cacheMgr.getCfMapDef(obj.getClass(), true); }
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()); }
@Before public void setupTest() { cacheMgr.initializeCacheForClass(MyTestBean.class); cacheMgr.initializeCacheForClass(MyCustomIdBean.class); cacheMgr.initializeCacheForClass(MyComplexEntity.class); cacheMgr.initializeCacheForClass(MyCompositeEntity.class); cacheMgr.initializeCacheForClass(MyConvertedCollectionBean.class); cacheMgr.initializeCacheForClass(AnonymousWithLongSerializer.class); }
@Test(expected = HectorObjectMapperException.class) public void testValidateDerivedClassMissingDiscriminatorValue() { ClassCacheMgr cacheMgr = new ClassCacheMgr(); CFMappingDef<MyTestBean> cfBaseMapDef = new CFMappingDef<MyTestBean>( MyTestBean.class); cfBaseMapDef.setInheritanceType(InheritanceType.SINGLE_TABLE); cfBaseMapDef.setDiscColumn("myType"); CFMappingDef<MyBlueTestBean> cfMapDef = new CFMappingDef<MyBlueTestBean>( MyBlueTestBean.class); cfMapDef.setCfSuperMapDef(cfBaseMapDef); cfMapDef.setCfBaseMapDef(cfBaseMapDef); InheritanceParserValidator val = new InheritanceParserValidator(); val.validateAndSetDefaults(cacheMgr, cfMapDef); fail("should have reported missing discriminator value annotation"); }