org.resthub.common.service.CrudServiceImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of resthub-common Show documentation
Show all versions of resthub-common Show documentation
RESThub core include Embeded datasource, Generic Repository and Generic CRUD services
package org.resthub.common.service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.io.Serializable;
import java.util.List;
/**
* CRUD service that uses a Spring Data repository implementation
* You should extend it and inject your Repository bean by overriding setRepository
*
* @param Your resource class to manage, usually an entity class
* @param Resource id type, usually Long or String
* @param The repository class
*/
@Transactional(readOnly = true)
public class CrudServiceImpl> implements
CrudService {
protected R repository;
/**
* @param repository
* the repository to set
*/
public void setRepository(R repository) {
this.repository = repository;
}
/**
* {@inheritDoc}
*/
@Override
@Transactional
public T create(T resource) {
Assert.notNull(resource, "Resource can't be null");
return repository.save(resource);
}
/**
* {@inheritDoc}
*/
@Override
@Transactional
public T update(T resource) {
Assert.notNull(resource, "Resource can't be null");
return repository.save(resource);
}
/**
* {@inheritDoc}
*/
@Override
@Transactional
public void delete(T resource) {
Assert.notNull(resource, "Resource can't be null");
repository.delete(resource);
}
/**
* {@inheritDoc}
*/
@Override
@Transactional
public void delete(ID id) {
Assert.notNull(id, "Resource ID can't be null");
repository.delete(id);
}
/**
* {@inheritDoc}
*/
@Override
@Transactional
public void deleteAll() {
repository.deleteAll();
}
/**
* {@inheritDoc}
*/
@Override
@Transactional
public void deleteAllWithCascade() {
Iterable list = repository.findAll();
for (T entity : list) {
repository.delete(entity);
}
}
/**
* {@inheritDoc}
*/
@Override
public T findById(ID id) {
Assert.notNull(id, "Resource ID can't be null");
return repository.findOne(id);
}
/**
* {@inheritDoc}
*/
@Override
public List findAll() {
return (List) repository.findAll();
}
/**
* {@inheritDoc}
*/
@Override
public Page findAll(Pageable pageRequest) {
return repository.findAll(pageRequest);
}
/**
* {@inheritDoc}
*/
@Override
public Long count() {
return repository.count();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy