Java Code Examples for org.springframework.orm.ObjectRetrievalFailureException

The following examples show how to use org.springframework.orm.ObjectRetrievalFailureException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Loads the {@link Owner} with the supplied <code>id</code>; also loads the {@link Pet Pets} and {@link Visit Visits}
 * for the corresponding owner, if not already loaded.
 */
@Override
public Owner findById(int id) throws DataAccessException {
    Owner owner;
    try {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        owner = this.namedParameterJdbcTemplate.queryForObject(
            "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id= :id",
            params,
            BeanPropertyRowMapper.newInstance(Owner.class)
        );
    } catch (EmptyResultDataAccessException ex) {
        throw new ObjectRetrievalFailureException(Owner.class, id);
    }
    loadPetsAndVisits(owner);
    return owner;
}
 
Example 2
Source Project: DevOps-for-Web-Development   Source File: JdbcPetRepositoryImpl.java    License: MIT License 6 votes vote down vote up
@Override
public Pet findById(int id) throws DataAccessException {
    JdbcPet pet;
    try {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        pet = this.namedParameterJdbcTemplate.queryForObject(
            "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id",
            params,
            new JdbcPetRowMapper());
    } catch (EmptyResultDataAccessException ex) {
        throw new ObjectRetrievalFailureException(Pet.class, id);
    }
    Owner owner = this.ownerRepository.findById(pet.getOwnerId());
    owner.addPet(pet);
    pet.setType(EntityUtils.getById(findPetTypes(), PetType.class, pet.getTypeId()));

    List<Visit> visits = this.visitRepository.findByPetId(pet.getId());
    for (Visit visit : visits) {
        pet.addVisit(visit);
    }
    return pet;
}
 
Example 3
/**
 * Loads the {@link Owner} with the supplied <code>id</code>; also loads the {@link Pet Pets} and {@link Visit Visits}
 * for the corresponding owner, if not already loaded.
 */
@Override
public Owner findById(int id) throws DataAccessException {
    Owner owner;
    try {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        owner = this.namedParameterJdbcTemplate.queryForObject(
            "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id= :id",
            params,
            BeanPropertyRowMapper.newInstance(Owner.class)
        );
    } catch (EmptyResultDataAccessException ex) {
        throw new ObjectRetrievalFailureException(Owner.class, id);
    }
    loadPetsAndVisits(owner);
    return owner;
}
 
Example 4
/**
 * Loads the {@link Owner} with the supplied <code>id</code>; also loads
 * the {@link Pet Pets} and {@link Visit Visits} for the corresponding
 * owner, if not already loaded.
 */
@Transactional(readOnly = true)
public Owner loadOwner(int id) throws DataAccessException {
	Owner owner;
	try {
		owner = this.simpleJdbcTemplate.queryForObject(
				"SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id=?",
				ParameterizedBeanPropertyRowMapper.newInstance(Owner.class),
				id);
	}
	catch (EmptyResultDataAccessException ex) {
		throw new ObjectRetrievalFailureException(Owner.class, new Integer(id));
	}
	loadPetsAndVisits(owner);
	return owner;
}
 
Example 5
@Transactional(readOnly = true)
public Pet loadPet(int id) throws DataAccessException {
	JdbcPet pet;
	try {
		pet = this.simpleJdbcTemplate.queryForObject(
				"SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=?",
				new JdbcPetRowMapper(),
				id);
	}
	catch (EmptyResultDataAccessException ex) {
		throw new ObjectRetrievalFailureException(Pet.class, new Integer(id));
	}
	Owner owner = loadOwner(pet.getOwnerId());
	owner.addPet(pet);
	pet.setType(EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId()));
	loadVisits(pet);
	return pet;
}
 
Example 6
Source Project: docker-workflow-plugin   Source File: JdbcOwnerRepositoryImpl.java    License: MIT License 6 votes vote down vote up
/**
 * Loads the {@link Owner} with the supplied <code>id</code>; also loads the {@link Pet Pets} and {@link Visit Visits}
 * for the corresponding owner, if not already loaded.
 */
@Override
public Owner findById(int id) throws DataAccessException {
    Owner owner;
    try {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        owner = this.namedParameterJdbcTemplate.queryForObject(
                "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id= :id",
                params,
                BeanPropertyRowMapper.newInstance(Owner.class)
        );
    } catch (EmptyResultDataAccessException ex) {
        throw new ObjectRetrievalFailureException(Owner.class, id);
    }
    loadPetsAndVisits(owner);
    return owner;
}
 
Example 7
Source Project: docker-workflow-plugin   Source File: JdbcPetRepositoryImpl.java    License: MIT License 6 votes vote down vote up
@Override
public Pet findById(int id) throws DataAccessException {
    JdbcPet pet;
    try {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        pet = this.namedParameterJdbcTemplate.queryForObject(
                "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id",
                params,
                new JdbcPetRowMapper());
    } catch (EmptyResultDataAccessException ex) {
        throw new ObjectRetrievalFailureException(Pet.class, new Integer(id));
    }
    Owner owner = this.ownerRepository.findById(pet.getOwnerId());
    owner.addPet(pet);
    pet.setType(EntityUtils.getById(findPetTypes(), PetType.class, pet.getTypeId()));

    List<Visit> visits = this.visitRepository.findByPetId(pet.getId());
    for (Visit visit : visits) {
        pet.addVisit(visit);
    }
    return pet;
}
 
Example 8
Source Project: audit4j-demo   Source File: JdbcOwnerRepositoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Loads the {@link Owner} with the supplied <code>id</code>; also loads the {@link Pet Pets} and {@link Visit Visits}
 * for the corresponding owner, if not already loaded.
 */
@Override
public Owner findById(int id) throws DataAccessException {
    Owner owner;
    try {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        owner = this.namedParameterJdbcTemplate.queryForObject(
                "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id= :id",
                params,
                BeanPropertyRowMapper.newInstance(Owner.class)
        );
    } catch (EmptyResultDataAccessException ex) {
        throw new ObjectRetrievalFailureException(Owner.class, id);
    }
    loadPetsAndVisits(owner);
    return owner;
}
 
Example 9
Source Project: audit4j-demo   Source File: JdbcPetRepositoryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Pet findById(int id) throws DataAccessException {
    JdbcPet pet;
    try {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        pet = this.namedParameterJdbcTemplate.queryForObject(
                "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id",
                params,
                new JdbcPetRowMapper());
    } catch (EmptyResultDataAccessException ex) {
        throw new ObjectRetrievalFailureException(Pet.class, new Integer(id));
    }
    Owner owner = this.ownerRepository.findById(pet.getOwnerId());
    owner.addPet(pet);
    pet.setType(EntityUtils.getById(findPetTypes(), PetType.class, pet.getTypeId()));

    List<Visit> visits = this.visitRepository.findByPetId(pet.getId());
    for (Visit visit : visits) {
        pet.addVisit(visit);
    }
    return pet;
}
 
Example 10
Source Project: rice   Source File: BusinessObjectDaoOjb.java    License: Educational Community License v2.0 6 votes vote down vote up
/**
 * @see org.kuali.rice.krad.dao.BusinessObjectDao#findBySinglePrimaryKey(java.lang.Class, java.lang.Object)
 */
public <T extends BusinessObject> T findBySinglePrimaryKey(Class<T> clazz, Object primaryKey) {
	if (primaryKey.getClass().getName().startsWith("java.lang.")
               || primaryKey.getClass().getName().startsWith("java.sql.")
               || primaryKey.getClass().getName().startsWith("java.math.")
               || primaryKey.getClass().getName().startsWith("java.util.")) {
		try {
			return (T) getPersistenceBrokerTemplate().getObjectById(clazz, primaryKey);
		} catch (  ObjectRetrievalFailureException ex  ) {
    		// it doesn't exist, just return null
			return null;
		}
	} else {
		Criteria criteria = buildCriteria(clazz, primaryKey);

        return (T) getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(clazz, criteria));
	}
}
 
Example 11
Source Project: spring-graalvm-native   Source File: EntityUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Look up the entity of the given class with the given id in the given collection.
 * @param entities the collection to search
 * @param entityClass the entity class to look up
 * @param entityId the entity id to look up
 * @return the found entity
 * @throws ObjectRetrievalFailureException if the entity was not found
 */
public static <T extends BaseEntity> T getById(Collection<T> entities, Class<T> entityClass, int entityId)
		throws ObjectRetrievalFailureException {
	for (T entity : entities) {
		if (entity.getId() == entityId && entityClass.isInstance(entity)) {
			return entity;
		}
	}
	throw new ObjectRetrievalFailureException(entityClass, entityId);
}
 
Example 12
Source Project: spring-init   Source File: EntityUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Look up the entity of the given class with the given id in the given collection.
 *
 * @param entities    the collection to search
 * @param entityClass the entity class to look up
 * @param entityId    the entity id to look up
 * @return the found entity
 * @throws ObjectRetrievalFailureException if the entity was not found
 */
public static <T extends BaseEntity> T getById(Collection<T> entities, Class<T> entityClass, int entityId)
    throws ObjectRetrievalFailureException {
    for (T entity : entities) {
        if (entity.getId() == entityId && entityClass.isInstance(entity)) {
            return entity;
        }
    }
    throw new ObjectRetrievalFailureException(entityClass, entityId);
}
 
Example 13
Source Project: DevOps-for-Web-Development   Source File: EntityUtils.java    License: MIT License 5 votes vote down vote up
/**
 * Look up the entity of the given class with the given id in the given collection.
 *
 * @param entities    the collection to search
 * @param entityClass the entity class to look up
 * @param entityId    the entity id to look up
 * @return the found entity
 * @throws ObjectRetrievalFailureException if the entity was not found
 */
public static <T extends BaseEntity> T getById(Collection<T> entities, Class<T> entityClass, int entityId)
    throws ObjectRetrievalFailureException {
    for (T entity : entities) {
        if (entity.getId() == entityId && entityClass.isInstance(entity)) {
            return entity;
        }
    }
    throw new ObjectRetrievalFailureException(entityClass, entityId);
}
 
Example 14
Source Project: DevOps-for-Web-Development   Source File: JdbcPetRepositoryImpl.java    License: MIT License 5 votes vote down vote up
@Override
public Pet findById(int id) throws DataAccessException {
    Integer ownerId;
    try {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        ownerId = this.namedParameterJdbcTemplate.queryForObject("SELECT owner_id FROM pets WHERE id=:id", params, Integer.class);
    } catch (EmptyResultDataAccessException ex) {
        throw new ObjectRetrievalFailureException(Pet.class, id);
    }
    Owner owner = this.ownerRepository.findById(ownerId);
    return EntityUtils.getById(owner.getPets(), Pet.class, id);
}
 
Example 15
Source Project: DevOps-for-Web-Development   Source File: EntityUtils.java    License: MIT License 5 votes vote down vote up
/**
 * Look up the entity of the given class with the given id in the given collection.
 *
 * @param entities    the collection to search
 * @param entityClass the entity class to look up
 * @param entityId    the entity id to look up
 * @return the found entity
 * @throws ObjectRetrievalFailureException if the entity was not found
 */
public static <T extends BaseEntity> T getById(Collection<T> entities, Class<T> entityClass, int entityId)
    throws ObjectRetrievalFailureException {
    for (T entity : entities) {
        if (entity.getId() == entityId && entityClass.isInstance(entity)) {
            return entity;
        }
    }
    throw new ObjectRetrievalFailureException(entityClass, entityId);
}
 
Example 16
/**
 * This method processes collections recursively up to the depth level specified
 * 
 * @param template
 * @param orig
 * @param copy
 */
private void processCollectionsRecurse(OjbCollectionAware template, PersistableBusinessObject orig, PersistableBusinessObject copy, int depth) {
    if (copy == null || depth < 1) {
        return;
    }

    List originalCollections = orig.buildListOfDeletionAwareLists();

    if (originalCollections != null && !originalCollections.isEmpty()) {
        /*
         * Prior to being saved, the version in the database will not yet reflect any deleted collections. So, a freshly
         * retrieved version will contain objects that need to be removed:
         */
        try {
            List copyCollections = copy.buildListOfDeletionAwareLists();
            int size = originalCollections.size();

            if (copyCollections.size() != size) {
                throw new RuntimeException("size mismatch while attempting to process list of Collections to manage");
            }

            for (int i = 0; i < size; i++) {
                Collection<PersistableBusinessObject> origSource = (Collection<PersistableBusinessObject>) originalCollections.get(i);
                Collection<PersistableBusinessObject> copySource = (Collection<PersistableBusinessObject>) copyCollections.get(i);
                List list = findUnwantedElements(copySource, origSource, template, depth - 1);
                cleanse(template, origSource, list);

            }
        }
        catch (ObjectRetrievalFailureException orfe) {
            // object wasn't found, must be pre-save
        }
    }
}
 
Example 17
/**
 * OJB RemovalAwareLists do not survive through the response/request lifecycle. This method is a work-around to forcibly remove
 * business objects that are found in Collections stored in the database but not in memory.
 * 
 * @param orig
 * @param id
 * @param template
 */
public void processCollections2(OjbCollectionAware template, PersistableBusinessObject orig, PersistableBusinessObject copy) {
    // if copy is null this is the first time we are saving the object, don't have to worry about updating collections
    if (copy == null) {
        return;
    }

    List originalCollections = orig.buildListOfDeletionAwareLists();

    if (originalCollections != null && !originalCollections.isEmpty()) {
        /*
         * Prior to being saved, the version in the database will not yet reflect any deleted collections. So, a freshly
         * retrieved version will contain objects that need to be removed:
         */
        try {
            List copyCollections = copy.buildListOfDeletionAwareLists();
            int size = originalCollections.size();

            if (copyCollections.size() != size) {
                throw new RuntimeException("size mismatch while attempting to process list of Collections to manage");
            }

            for (int i = 0; i < size; i++) {
                Collection origSource = (Collection) originalCollections.get(i);
                Collection copySource = (Collection) copyCollections.get(i);
                List list = findUnwantedElements(copySource, origSource, null, 0);
                cleanse(template, origSource, list);

            }
        }
        catch (ObjectRetrievalFailureException orfe) {
            // object wasn't found, must be pre-save
        }
    }
}
 
Example 18
Source Project: rice   Source File: OjbCollectionHelper.java    License: Educational Community License v2.0 5 votes vote down vote up
/**
 * OJB RemovalAwareLists do not survive through the response/request lifecycle. This method is a work-around to forcibly remove
 * business objects that are found in Collections stored in the database but not in memory.
 * 
 * @param orig
 * @param id
 * @param template
 */
public void processCollections(OjbCollectionAware template, PersistableBusinessObject orig, PersistableBusinessObject copy) {
    if (copy == null) {
        return;
    }
    
    List<Collection<PersistableBusinessObject>> originalCollections = orig.buildListOfDeletionAwareLists();

    if (originalCollections != null && !originalCollections.isEmpty()) {
        /*
         * Prior to being saved, the version in the database will not yet reflect any deleted collections. So, a freshly
         * retrieved version will contain objects that need to be removed:
         */
        try {
            List<Collection<PersistableBusinessObject>> copyCollections = copy.buildListOfDeletionAwareLists();
            int size = originalCollections.size();

            if (copyCollections.size() != size) {
                throw new RuntimeException("size mismatch while attempting to process list of Collections to manage");
            }

            for (int i = 0; i < size; i++) {
                Collection<PersistableBusinessObject> origSource = originalCollections.get(i);
                Collection<PersistableBusinessObject> copySource = copyCollections.get(i);
                List<PersistableBusinessObject> list = findUnwantedElements(copySource, origSource);
                cleanse(template, origSource, list);
            }
        }
        catch (ObjectRetrievalFailureException orfe) {
            // object wasn't found, must be pre-save
        }
    }
}
 
Example 19
Source Project: rice   Source File: OjbCollectionHelper.java    License: Educational Community License v2.0 5 votes vote down vote up
/**
 * OJB RemovalAwareLists do not survive through the response/request lifecycle. This method is a work-around to forcibly remove
 * business objects that are found in Collections stored in the database but not in memory.
 * 
 * @param orig
 * @param id
 * @param template
 */
public void processCollections2(OjbCollectionAware template, PersistableBusinessObject orig, PersistableBusinessObject copy) {
    // if copy is null this is the first time we are saving the object, don't have to worry about updating collections
    if (copy == null) {
        return;
    }
    
    List<Collection<PersistableBusinessObject>> originalCollections = orig.buildListOfDeletionAwareLists();

    if (originalCollections != null && !originalCollections.isEmpty()) {
        /*
         * Prior to being saved, the version in the database will not yet reflect any deleted collections. So, a freshly
         * retrieved version will contain objects that need to be removed:
         */
        try {
            List<Collection<PersistableBusinessObject>> copyCollections = copy.buildListOfDeletionAwareLists();
            int size = originalCollections.size();

            if (copyCollections.size() != size) {
                throw new RuntimeException("size mismatch while attempting to process list of Collections to manage");
            }

            for (int i = 0; i < size; i++) {
                Collection<PersistableBusinessObject> origSource = originalCollections.get(i);
                Collection<PersistableBusinessObject> copySource = copyCollections.get(i);
                List<PersistableBusinessObject> list = findUnwantedElements(copySource, origSource);
                cleanse(template, origSource, list);
            }
        }
        catch (ObjectRetrievalFailureException orfe) {
            // object wasn't found, must be pre-save
        }
    }
}
 
Example 20
@Override
public PeopleFlowDefinition getPeopleFlow(@WebParam(name = "peopleFlowId") String peopleFlowId) throws RiceIllegalArgumentException {
    if (validPeopleFlowIds.contains(peopleFlowId)) {
        return PeopleFlowDefinition.Builder.create("myNamespace", "myPeopleFlowName").build();
    } else {
        // simulate what our PeopleFlowServiceImpl would do
        throw new ObjectRetrievalFailureException("", new RuntimeException());
    }
}
 
Example 21
Source Project: jdal   Source File: HibernateDao.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public T get(PK id) {
    T entity = (T) getHibernateTemplate().get(this.entityClass, id);

    if (entity == null) {
        log.warn("'" + this.entityClass.getSimpleName() + "' object with id '" + id + "' not found...");
        throw new ObjectRetrievalFailureException(this.entityClass, id);
    }

    return entity;
}
 
Example 22
@RequestMapping(produces = {Versions.V1_0, Versions.V2_0})
@ExceptionHandler(ObjectRetrievalFailureException.class)
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public @ResponseBody Map<String, Object> handleValidationException(ObjectRetrievalFailureException ex) throws IOException {
    Map<String, Object>  map = Maps.newHashMap();
    map.put("error", "Entity Not Found");
    map.put("cause", ex.getMessage());
    return map;
}
 
Example 23
public Ticket get(final long id) {
    Ticket ticket = ticketDao.findOne(id);
    if (ticket != null) {
        return ticket;
    }
    throw new ObjectRetrievalFailureException(Ticket.class, id);
}
 
Example 24
public Location addLocationScan(Ticket ticket, Long locationId) {
    Location location = locationDao.findOne(locationId);
    if (location == null) {
        throw new ObjectRetrievalFailureException(Ticket.class, locationId);
    }

    LocationScan locationScan = new LocationScan();
    locationScan.setLocation(location);
    locationScan.setTimestamp(new Date());
    ticket.getLocationScans().add(locationScan);
    store(ticket);

    return location;
}
 
Example 25
public void deleteLocationScan(Ticket ticket, Long locationId) {

        for (Iterator<LocationScan> locationIterator = ticket.getLocationScans().iterator() ; locationIterator.hasNext(); ) {
            LocationScan location = locationIterator.next();
            if (locationId.equals(location.getId())) {
                locationIterator.remove();
                return;
            }
        }

        throw new ObjectRetrievalFailureException(Location.class, locationId);
    }
 
Example 26
/**
 * Look up the entity of the given class with the given id in the given
 * collection.
 *
 * @param entities the collection to search
 * @param entityClass the entity class to look up
 * @param entityId the entity id to look up
 * @return the found entity
 * @throws ObjectRetrievalFailureException if the entity was not found
 */
public static <T extends BaseEntity> T getById(Collection<T> entities, Class<T> entityClass, int entityId)
		throws ObjectRetrievalFailureException {
	for (T entity : entities) {
		if (entity.getId().intValue() == entityId && entityClass.isInstance(entity)) {
			return entity;
		}
	}
	throw new ObjectRetrievalFailureException(entityClass, new Integer(entityId));
}
 
Example 27
Source Project: docker-workflow-plugin   Source File: EntityUtils.java    License: MIT License 3 votes vote down vote up
/**
 * Look up the entity of the given class with the given id in the given collection.
 *
 * @param entities    the collection to search
 * @param entityClass the entity class to look up
 * @param entityId    the entity id to look up
 * @return the found entity
 * @throws ObjectRetrievalFailureException
 *          if the entity was not found
 */
public static <T extends BaseEntity> T getById(Collection<T> entities, Class<T> entityClass, int entityId)
        throws ObjectRetrievalFailureException {
    for (T entity : entities) {
        if (entity.getId().intValue() == entityId && entityClass.isInstance(entity)) {
            return entity;
        }
    }
    throw new ObjectRetrievalFailureException(entityClass, entityId);
}
 
Example 28
Source Project: audit4j-demo   Source File: EntityUtils.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Look up the entity of the given class with the given id in the given collection.
 *
 * @param entities    the collection to search
 * @param entityClass the entity class to look up
 * @param entityId    the entity id to look up
 * @return the found entity
 * @throws ObjectRetrievalFailureException
 *          if the entity was not found
 */
public static <T extends BaseEntity> T getById(Collection<T> entities, Class<T> entityClass, int entityId)
        throws ObjectRetrievalFailureException {
    for (T entity : entities) {
        if (entity.getId().intValue() == entityId && entityClass.isInstance(entity)) {
            return entity;
        }
    }
    throw new ObjectRetrievalFailureException(entityClass, entityId);
}
 
Example 29
Source Project: activejpa   Source File: EntityUtils.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Look up the entity of the given class with the given id in the given collection.
 *
 * @param entities    the collection to search
 * @param entityClass the entity class to look up
 * @param entityId    the entity id to look up
 * @return the found entity
 * @throws ObjectRetrievalFailureException
 *          if the entity was not found
 */
public static <T extends BaseEntity> T getById(Collection<T> entities, Class<T> entityClass, int entityId)
        throws ObjectRetrievalFailureException {
    for (T entity : entities) {
        if (entity.getId().intValue() == entityId && entityClass.isInstance(entity)) {
            return entity;
        }
    }
    throw new ObjectRetrievalFailureException(entityClass, entityId);
}