public void assertUpdateableIdIfNotSet() { if (!entity.hasIdProperty()) { return; } MongoPersistentProperty property = entity.getRequiredIdProperty(); Object propertyValue = idAccessor.getIdentifier(); if (propertyValue != null) { return; } if (!MongoSimpleTypes.AUTOGENERATED_ID_TYPES.contains(property.getType())) { throw new InvalidDataAccessApiUsageException( String.format("Cannot autogenerate id of type %s for entity of type %s!", property.getType().getName(), entity.getType().getName())); } }
MongoPersistentEntity<?> entity = valueType.isSubTypeOf(prop.getType()) ? mappingContext.getRequiredPersistentEntity(obj.getClass()) : mappingContext.getRequiredPersistentEntity(type);
MongoPersistentEntity<?> entity = isSubTypeOf(obj.getClass(), prop.getType()) ? mappingContext.getRequiredPersistentEntity(obj.getClass()) : mappingContext.getRequiredPersistentEntity(type);
private void assertUpdateableIdIfNotSet(Object value) { MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(value.getClass()); if (entity != null && entity.hasIdProperty()) { MongoPersistentProperty property = entity.getRequiredIdProperty(); Object propertyValue = entity.getPropertyAccessor(value).getProperty(property); if (propertyValue != null) { return; } if (!MongoSimpleTypes.AUTOGENERATED_ID_TYPES.contains(property.getType())) { throw new InvalidDataAccessApiUsageException( String.format("Cannot autogenerate id of type %s for entity of type %s!", property.getType().getName(), value.getClass().getName())); } } }
@Override public T initializeVersionProperty() { if (!entity.hasVersionProperty()) { return propertyAccessor.getBean(); } MongoPersistentProperty versionProperty = entity.getRequiredVersionProperty(); propertyAccessor.setProperty(versionProperty, versionProperty.getType().isPrimitive() ? 1 : 0); return propertyAccessor.getBean(); }
protected DBRef createDBRef(Object target, MongoPersistentProperty property) { Assert.notNull(target, "Target object must not be null!"); if (target instanceof DBRef) { return (DBRef) target; } MongoPersistentEntity<?> targetEntity = mappingContext.getPersistentEntity(target.getClass()); targetEntity = targetEntity != null ? targetEntity : mappingContext.getPersistentEntity(property); if (null == targetEntity) { throw new MappingException("No mapping metadata found for " + target.getClass()); } MongoPersistentEntity<?> entity = targetEntity; MongoPersistentProperty idProperty = entity.getIdProperty(); if (idProperty != null) { Object id = target.getClass().equals(idProperty.getType()) ? target : entity.getPropertyAccessor(target).getProperty(idProperty); if (null == id) { throw new MappingException("Cannot create a reference to an object with a NULL id."); } return dbRefResolver.createDbRef(property == null ? null : property.getDBRef(), entity, idMapper.convertId(id, idProperty != null ? idProperty.getFieldType() : ObjectId.class)); } throw new MappingException("No id property found on class " + entity.getType()); }
/** * Creates a proxy for the given {@link MongoPersistentProperty} using the given {@link DbRefResolverCallback} to * eventually resolve the value of the property. * * @param property must not be {@literal null}. * @param dbref can be {@literal null}. * @param callback must not be {@literal null}. * @return */ private Object createLazyLoadingProxy(MongoPersistentProperty property, @Nullable DBRef dbref, DbRefResolverCallback callback, DbRefProxyHandler handler) { Class<?> propertyType = property.getType(); LazyLoadingInterceptor interceptor = new LazyLoadingInterceptor(property, dbref, exceptionTranslator, callback); if (!propertyType.isInterface()) { Factory factory = (Factory) objenesis.newInstance(getEnhancedTypeFor(propertyType)); factory.setCallbacks(new Callback[] { interceptor }); return handler.populateId(property, dbref, factory); } ProxyFactory proxyFactory = new ProxyFactory(); for (Class<?> type : propertyType.getInterfaces()) { proxyFactory.addInterface(type); } proxyFactory.addInterface(LazyLoadingProxy.class); proxyFactory.addInterface(propertyType); proxyFactory.addAdvice(interceptor); return handler.populateId(property, dbref, proxyFactory.getProxy()); }
public void assertUpdateableIdIfNotSet() { if (!entity.hasIdProperty()) { return; } MongoPersistentProperty property = entity.getRequiredIdProperty(); Object propertyValue = idAccessor.getIdentifier(); if (propertyValue != null) { return; } if (!MongoSimpleTypes.AUTOGENERATED_ID_TYPES.contains(property.getType())) { throw new InvalidDataAccessApiUsageException( String.format("Cannot autogenerate id of type %s for entity of type %s!", property.getType().getName(), entity.getType().getName())); } }
private void assertUpdateableIdIfNotSet(Object value) { MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(value.getClass()); if (entity != null && entity.hasIdProperty()) { MongoPersistentProperty property = entity.getRequiredIdProperty(); Object propertyValue = entity.getPropertyAccessor(value).getProperty(property); if (propertyValue != null) { return; } if (!MongoSimpleTypes.AUTOGENERATED_ID_TYPES.contains(property.getType())) { throw new InvalidDataAccessApiUsageException( String.format("Cannot autogenerate id of type %s for entity of type %s!", property.getType().getName(), value.getClass().getName())); } } }
@Override public T initializeVersionProperty() { if (!entity.hasVersionProperty()) { return propertyAccessor.getBean(); } MongoPersistentProperty versionProperty = entity.getRequiredVersionProperty(); propertyAccessor.setProperty(versionProperty, versionProperty.getType().isPrimitive() ? 1 : 0); return propertyAccessor.getBean(); }
protected DBRef createDBRef(Object target, MongoPersistentProperty property) { Assert.notNull(target, "Target object must not be null!"); if (target instanceof DBRef) { return (DBRef) target; } MongoPersistentEntity<?> targetEntity = mappingContext.getPersistentEntity(target.getClass()); targetEntity = targetEntity != null ? targetEntity : mappingContext.getPersistentEntity(property); if (null == targetEntity) { throw new MappingException("No mapping metadata found for " + target.getClass()); } MongoPersistentEntity<?> entity = targetEntity; MongoPersistentProperty idProperty = entity.getIdProperty(); if (idProperty != null) { Object id = target.getClass().equals(idProperty.getType()) ? target : entity.getPropertyAccessor(target).getProperty(idProperty); if (null == id) { throw new MappingException("Cannot create a reference to an object with a NULL id."); } return dbRefResolver.createDbRef(property == null ? null : property.getDBRef(), entity, idMapper.convertId(id)); } throw new MappingException("No id property found on class " + entity.getType()); }
/** * Creates a proxy for the given {@link MongoPersistentProperty} using the given {@link DbRefResolverCallback} to * eventually resolve the value of the property. * * @param property must not be {@literal null}. * @param dbref can be {@literal null}. * @param callback must not be {@literal null}. * @return */ private Object createLazyLoadingProxy(MongoPersistentProperty property, @Nullable DBRef dbref, DbRefResolverCallback callback, DbRefProxyHandler handler) { Class<?> propertyType = property.getType(); LazyLoadingInterceptor interceptor = new LazyLoadingInterceptor(property, dbref, exceptionTranslator, callback); if (!propertyType.isInterface()) { Factory factory = (Factory) objenesis.newInstance(getEnhancedTypeFor(propertyType)); factory.setCallbacks(new Callback[] { interceptor }); return handler.populateId(property, dbref, factory); } ProxyFactory proxyFactory = new ProxyFactory(); for (Class<?> type : propertyType.getInterfaces()) { proxyFactory.addInterface(type); } proxyFactory.addInterface(LazyLoadingProxy.class); proxyFactory.addInterface(propertyType); proxyFactory.addAdvice(interceptor); return handler.populateId(property, dbref, proxyFactory.getProxy()); }
/** * Creates a proxy for the given {@link MongoPersistentProperty} using the given {@link DbRefResolverCallback} to * eventually resolve the value of the property. * * @param property must not be {@literal null}. * @param dbref can be {@literal null}. * @param callback must not be {@literal null}. * @return */ private Object createLazyLoadingProxy(MongoPersistentProperty property, DBRef dbref, DbRefResolverCallback callback, DbRefProxyHandler handler) { Class<?> propertyType = property.getType(); LazyLoadingInterceptor interceptor = new LazyLoadingInterceptor(property, dbref, exceptionTranslator, callback); if (!propertyType.isInterface()) { Factory factory = (Factory) objenesis.newInstance(getEnhancedTypeFor(propertyType)); factory.setCallbacks(new Callback[] { interceptor }); return handler.populateId(property, dbref, factory); } ProxyFactory proxyFactory = new ProxyFactory(); for (Class<?> type : propertyType.getInterfaces()) { proxyFactory.addInterface(type); } proxyFactory.addInterface(LazyLoadingProxy.class); proxyFactory.addInterface(propertyType); proxyFactory.addAdvice(interceptor); return handler.populateId(property, dbref, proxyFactory.getProxy()); }