diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/CrudRepositoryInvoker.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/CrudRepositoryInvoker.java index f850dc7c4..82fb74c05 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/CrudRepositoryInvoker.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/CrudRepositoryInvoker.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,11 +16,13 @@ package org.springframework.data.rest.core.invoke; import java.io.Serializable; +import java.lang.reflect.Method; import org.springframework.core.convert.ConversionService; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.core.CrudMethods; import org.springframework.data.repository.core.RepositoryInformation; /** @@ -32,6 +34,11 @@ import org.springframework.data.repository.core.RepositoryInformation; class CrudRepositoryInvoker extends ReflectionRepositoryInvoker { private final CrudRepository repository; + private final CrudMethods crudMethods; + + private final boolean customSaveMethod; + private final boolean customFindOneMethod; + private final boolean customDeleteMethod; /** * Creates a new {@link CrudRepositoryInvoker} for the given {@link CrudRepository}, {@link RepositoryInformation} and @@ -46,6 +53,11 @@ class CrudRepositoryInvoker extends ReflectionRepositoryInvoker { super(repository, information, conversionService); this.repository = repository; + this.crudMethods = information.getCrudMethods(); + + this.customSaveMethod = isRedeclaredMethod(crudMethods.getSaveMethod()); + this.customFindOneMethod = isRedeclaredMethod(crudMethods.getFindOneMethod()); + this.customDeleteMethod = isRedeclaredMethod(crudMethods.getDeleteMethod()); } /** @@ -80,8 +92,9 @@ class CrudRepositoryInvoker extends ReflectionRepositoryInvoker { * @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeFindOne(java.io.Serializable) */ @Override - public Object invokeFindOne(Serializable id) { - return repository.findOne(convertId(id)); + @SuppressWarnings("unchecked") + public T invokeFindOne(Serializable id) { + return customFindOneMethod ? super. invokeFindOne(id) : (T) repository.findOne(convertId(id)); } /* @@ -89,8 +102,8 @@ class CrudRepositoryInvoker extends ReflectionRepositoryInvoker { * @see org.springframework.data.rest.core.invoke.ReflectionRepositoryInvoker#invokeSave(java.lang.Object) */ @Override - public Object invokeSave(Object entity) { - return repository.save(entity); + public T invokeSave(T entity) { + return customSaveMethod ? super.invokeSave(entity) : repository.save(entity); } /* @@ -99,6 +112,15 @@ class CrudRepositoryInvoker extends ReflectionRepositoryInvoker { */ @Override public void invokeDelete(Serializable id) { - repository.delete(convertId(id)); + + if (customDeleteMethod) { + super.invokeDelete(id); + } else { + repository.delete(convertId(id)); + } + } + + private boolean isRedeclaredMethod(Method method) { + return !method.getDeclaringClass().equals(CrudRepository.class); } } diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvoker.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvoker.java index 7aeb94f9c..09e5c1768 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvoker.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvoker.java @@ -128,7 +128,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker { * @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeSave(java.lang.Object) */ @Override - public Object invokeSave(Object object) { + public T invokeSave(T object) { return invoke(methods.getSaveMethod(), object); } @@ -146,7 +146,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker { * @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeFindOne(java.io.Serializable) */ @Override - public Object invokeFindOne(Serializable id) { + public T invokeFindOne(Serializable id) { return invoke(methods.getFindOneMethod(), convertId(id)); } diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/RepositoryInvoker.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/RepositoryInvoker.java index db370a339..70ffa05d9 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/RepositoryInvoker.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/RepositoryInvoker.java @@ -27,9 +27,9 @@ import org.springframework.data.domain.Sort; */ public interface RepositoryInvoker extends RepositoryInvocationInformation { - Object invokeSave(Object object); + T invokeSave(T object); - Object invokeFindOne(Serializable id); + T invokeFindOne(Serializable id); Iterable invokeFindAll(Pageable pageable); diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/Order.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/Order.java index 5e96914ea..c543a15ef 100644 --- a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/Order.java +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/Order.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.data.rest.core.domain.jpa; import javax.persistence.Entity; +import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.Table; @@ -27,7 +28,7 @@ import javax.persistence.Table; @Table(name = "ORDERS") public class Order { - private @Id Long id; + private @Id @GeneratedValue Long id; private @ManyToOne Person creator; public Order(Person creator) { diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/OrderRepository.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/OrderRepository.java index 9d58b778c..a5496ceb2 100644 --- a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/OrderRepository.java +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/OrderRepository.java @@ -22,4 +22,17 @@ import org.springframework.data.repository.CrudRepository; */ public interface OrderRepository extends CrudRepository { + /* + * (non-Javadoc) + * @see org.springframework.data.repository.CrudRepository#save(java.lang.Object) + */ + @Override + public S save(S entity); + + /* + * (non-Javadoc) + * @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable) + */ + @Override + public Order findOne(Long id); }