/** * Determines if the key is complex (IdClass, Embedded, etc) or a simple one field type. * * @return true if complex, false otherwise */ public boolean isComplexKey() { return null != getPkClazz(); }
@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); } } }
private <T> void setComplexId(CFMappingDef<T> cfMapDef, T obj, Object pkObj) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { KeyDefinition keyDef = cfMapDef.getKeyDef(); if (!pkObj.getClass().equals(keyDef.getPkClazz())) { throw new HectorObjectMapperException("primary key object, " + pkObj.getClass().getName() + ", must be of type " + keyDef.getPkClazz().getName()); } for (PropertyDescriptor pd : keyDef.getPropertyDescriptorMap().values()) { PropertyMappingDefinition md = keyDef.getIdPropertyMap().get(pd.getName()); if (null == md) { throw new HectorObjectMapperException("Trying to set complex key type, but field, " + pd.getName() + ", is not annotated with @" + Id.class.getSimpleName() + " in POJO, " + cfMapDef.getRealClass().getName()); } Method meth = md.getPropDesc().getWriteMethod(); if (null == meth) { logger.debug("@Id annotation found - but can't find setter for property, " + md.getPropDesc().getName()); } meth.invoke(obj, pd.getReadMethod().invoke(pkObj, (Object[]) null)); } }
@Test public void testParsingComplexEntity() { ClassCacheMgr cacheMgr = new ClassCacheMgr(); CFMappingDef<MyComplexEntity> cfMapDef = cacheMgr.initializeCacheForClass(MyComplexEntity.class); KeyDefinition keyDef = cfMapDef.getKeyDef(); assertEquals(MyCompositePK.class, keyDef.getPkClazz()); assertEquals(2, keyDef.getIdPropertyMap().size()); assertEquals(keyDef.getIdPropertyMap().size(), keyDef.getPropertyDescriptorMap().size()); }