Search in sources :

Example 26 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project spring-data-mongodb by spring-projects.

the class MongoPersistentEntityIndexCreator method createIndex.

void createIndex(IndexDefinitionHolder indexDefinition) {
    try {
        IndexOperations indexOperations = indexOperationsProvider.indexOps(indexDefinition.getCollection());
        indexOperations.ensureIndex(indexDefinition);
    } catch (UncategorizedMongoDbException ex) {
        if (ex.getCause() instanceof MongoException && MongoDbErrorCodes.isDataIntegrityViolationCode(((MongoException) ex.getCause()).getCode())) {
            IndexInfo existingIndex = fetchIndexInformation(indexDefinition);
            String message = "Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'.";
            if (existingIndex != null) {
                message += " Index already defined as '%s'.";
            }
            throw new DataIntegrityViolationException(String.format(message, indexDefinition.getPath(), indexDefinition.getCollection(), indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions(), existingIndex), ex.getCause());
        }
        throw ex;
    }
}
Also used : MongoException(com.mongodb.MongoException) UncategorizedMongoDbException(org.springframework.data.mongodb.UncategorizedMongoDbException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 27 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project spring-data-mongodb by spring-projects.

the class MongoChangeSetPersister method getPersistentState.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.crossstore.ChangeSetPersister#getPersistentState(java.lang.Class, java.lang.Object, org.springframework.data.crossstore.ChangeSet)
	 */
public void getPersistentState(Class<? extends ChangeSetBacked> entityClass, Object id, final ChangeSet changeSet) throws DataAccessException, NotFoundException {
    if (id == null) {
        log.debug("Unable to load MongoDB data for null id");
        return;
    }
    String collName = getCollectionNameForEntity(entityClass);
    final Document dbk = new Document();
    dbk.put(ENTITY_ID, id);
    dbk.put(ENTITY_CLASS, entityClass.getName());
    if (log.isDebugEnabled()) {
        log.debug("Loading MongoDB data for {}", dbk);
    }
    mongoTemplate.execute(collName, new CollectionCallback<Object>() {

        public Object doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
            for (Document dbo : collection.find(dbk)) {
                String key = (String) dbo.get(ENTITY_FIELD_NAME);
                if (log.isDebugEnabled()) {
                    log.debug("Processing key: {}", key);
                }
                if (!changeSet.getValues().containsKey(key)) {
                    String className = (String) dbo.get(ENTITY_FIELD_CLASS);
                    if (className == null) {
                        throw new DataIntegrityViolationException("Unble to convert property " + key + ": Invalid metadata, " + ENTITY_FIELD_CLASS + " not available");
                    }
                    Class<?> clazz = ClassUtils.resolveClassName(className, ClassUtils.getDefaultClassLoader());
                    Object value = mongoTemplate.getConverter().read(clazz, dbo);
                    if (log.isDebugEnabled()) {
                        log.debug("Adding to ChangeSet: {}", key);
                    }
                    changeSet.set(key, value);
                }
            }
            return null;
        }
    });
}
Also used : MongoException(com.mongodb.MongoException) Document(org.bson.Document) DataAccessException(org.springframework.dao.DataAccessException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Aggregations

DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)27 Test (org.junit.Test)11 SQLException (java.sql.SQLException)8 HashSet (java.util.HashSet)6 HashMap (java.util.HashMap)5 Set (java.util.Set)5 DataAccessException (org.springframework.dao.DataAccessException)5 SimpleJdbcInsert (org.springframework.jdbc.core.simple.SimpleJdbcInsert)5 ResultSet (java.sql.ResultSet)4 Date (java.util.Date)3 List (java.util.List)3 DeadlockLoserDataAccessException (org.springframework.dao.DeadlockLoserDataAccessException)3 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)3 MongoException (com.mongodb.MongoException)2 CabinetException (cz.metacentrum.perun.cabinet.bl.CabinetException)2 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 ConsistencyErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.ConsistencyErrorRuntimeException)2 InternalErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException)2 ArrayList (java.util.ArrayList)2 ObjectId (org.bson.types.ObjectId)2