I\'m considering spring data for a project. Is it possible to override the per default generated save method? And if yes, how?
In order to properly override the save method, you have to create a interface with the proper signature of the original method declared on CrudRepository, including the generics
public interface MyCustomRepository {
S save(S entity);
}
Then, create your implementation ( the suffix Impl is important at the name of the class)
public class MyCustomRepositoryImpl implements MyCustomRepository {
@Autowired
private EntityManager entityManager;
@Override
public S save(S entity) {
/**
your custom implementation comes here ...
i think the default one is just
return this.entityManager.persist(entity);
*/
}
}
Finally, extend your repository with the previously created interface
@RepositoryRestResource
@Repository
public interface MyBeanRepository extends PagingAndSortingRepository, MyCustomRepository {}