List of usage examples for org.springframework.dao DataIntegrityViolationException DataIntegrityViolationException
public DataIntegrityViolationException(String msg)
From source file:org.springframework.data.mongodb.core.MongoTemplate.java
/** * Checks and handles any errors./*from ww w . j av a 2 s . c o m*/ * <p/> * Current implementation logs errors. Future version may make this configurable to log warning, errors or throw * exception. */ protected void handleAnyWriteResultErrors(WriteResult wr, DBObject query, String operation) { if (WriteResultChecking.NONE == this.writeResultChecking) { return; } String error = wr.getError(); if (error != null) { String message; if (operation.equals("insert") || operation.equals("save")) { // assuming the insert operations will begin with insert string message = String.format("Insert/Save for %s failed: %s", query, error); } else if (operation.equals("insert_list")) { message = String.format("Insert list failed: %s", error); } else { message = String.format("Execution of %s%s failed: %s", operation, query == null ? "" : "' using '" + query.toString() + "' query", error); } if (WriteResultChecking.EXCEPTION == this.writeResultChecking) { throw new DataIntegrityViolationException(message); } else { LOGGER.error(message); return; } } }
From source file:org.springframework.data.mongodb.crossstore.MongoChangeSetPersister.java
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;// w w w . ja v a2 s. c om } String collName = getCollectionNameForEntity(entityClass); final DBObject dbk = new BasicDBObject(); 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(DBCollection collection) throws MongoException, DataAccessException { for (DBObject 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; } }); }
From source file:org.springframework.jdbc.core.simple.AbstractJdbcInsert.java
/** * Delegate method to execute the insert, generating a single key. *//*from ww w.j av a 2 s . com*/ private Number executeInsertAndReturnKeyInternal(final List<?> values) { KeyHolder kh = executeInsertAndReturnKeyHolderInternal(values); if (kh.getKey() != null) { return kh.getKey(); } else { throw new DataIntegrityViolationException( "Unable to retrieve the generated key for the insert: " + getInsertString()); } }