From 0031cc37fa52d9456c8d2acd721363af6aa5f487 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 13 Nov 2013 09:53:06 +0100 Subject: [PATCH] DATAGRAPH-409 - Improved default transaction configuration for repositories. Moved the default transaction configuration from CRUDRepository interface to AbstractGraphRepository to align with transaction configuration with other Spring Data Modules. Removed obsolete method re-declaration from CRUDRepository. CRUDRepository was missing a redeclaration of delete(Serializable id) so that deletes by id didn't trigger a default transaction. This caused e.g. DATAREST-184. --- .../repository/AbstractGraphRepository.java | 9 +- .../data/neo4j/repository/CRUDRepository.java | 97 ++----------------- 2 files changed, 16 insertions(+), 90 deletions(-) diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/AbstractGraphRepository.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/AbstractGraphRepository.java index 13f0735c4..50cdc48af 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/AbstractGraphRepository.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/AbstractGraphRepository.java @@ -41,6 +41,7 @@ import org.springframework.data.neo4j.support.index.NoSuchIndexException; import org.springframework.data.neo4j.support.index.NullReadableIndex; import org.springframework.data.neo4j.support.query.QueryEngine; import org.springframework.data.neo4j.support.typerepresentation.LabelBasedNodeTypeRepresentationStrategy; +import org.springframework.transaction.annotation.Transactional; import java.util.*; @@ -54,6 +55,7 @@ import static org.neo4j.helpers.collection.MapUtil.map; * @param GraphBacked target of this finder, enables the finder methods to return this concrete type * @param Type of backing state, either Node or Relationship */ +@Transactional(readOnly = true) public abstract class AbstractGraphRepository implements GraphRepository, NamedIndexRepository, SpatialRepository, CypherDslRepository { /* @@ -108,12 +110,13 @@ public abstract class AbstractGraphRepository im } @Override + @Transactional public U save(U entity) { return template.save(entity); } - @SuppressWarnings("unchecked") @Override + @Transactional public Iterable save(Iterable entities) { for (U entity : entities) { save(entity); @@ -326,16 +329,19 @@ public abstract class AbstractGraphRepository im } @Override + @Transactional public void delete(T entity) { template.delete(entity); } @Override + @Transactional public void delete(Long id) { delete(findOne(id)); } @Override + @Transactional public void delete(Iterable entities) { for (T entity : entities) { delete(entity); @@ -343,6 +349,7 @@ public abstract class AbstractGraphRepository im } @Override + @Transactional public void deleteAll() { delete(findAll()); } diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/CRUDRepository.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/CRUDRepository.java index d8f00a333..132e45496 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/CRUDRepository.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/CRUDRepository.java @@ -16,98 +16,30 @@ package org.springframework.data.neo4j.repository; -import org.neo4j.helpers.collection.ClosableIterable; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Sort; +import java.util.Map; +import org.springframework.data.domain.Sort; import org.springframework.data.neo4j.conversion.EndResult; import org.springframework.data.repository.NoRepositoryBean; import org.springframework.data.repository.PagingAndSortingRepository; -import org.springframework.transaction.annotation.Transactional; - -import java.util.Map; /** - * CRUD interface for graph repositories, used as base repository for crud operations + * CRUD interface for graph repositories, used as base repository for crud operations. + * + * @author Michael Hunger + * @author Oliver Gierke */ @NoRepositoryBean public interface CRUDRepository extends PagingAndSortingRepository { - /** - * persists an entity by forwarding to entity.persist() - * @param entity to be persisted - * @return the saved entity (being the same reference as the parameter) - */ - @Transactional - U save(U entity); - - - /** - * persists the provided entities by forwarding to their entity.persist() methods - * @param entities to be persisted - * @return the input iterable - */ - @Transactional - Iterable save(Iterable entities); - - - /** - * @param id of the node or relationship-entity - * @return found instance or null - */ - @Transactional - T findOne(Long id); - - - /** - * @param id - * @return true if the entity with this id exists - */ - @Transactional - boolean exists(Long id); - /** * uses the configured TypeRepresentationStrategy to load all entities, might return a large result * @return all entities of the given type * NOTE: please close the iterable if it is not fully looped through */ - @Transactional EndResult findAll(); - - - /** - * uses the configured TypeRepresentationStrategy, depending on the strategy this number might be an - * approximation - * @return number of entities of this type in the graph - */ - @Transactional - long count(); - - - /** - * deletes the given entity by calling its entity.remove() method - * @param entity to delete - */ - @Transactional - void delete(T entity); - - - /** - * deletes the given entities by calling their entity.remove() methods - * @param entities to delete - */ - @Transactional - void delete(Iterable entities); - - - /** - * removes all entities of this type, use with care - */ - @Transactional - void deleteAll(); - + /** * finder that takes the provided sorting into account @@ -116,24 +48,11 @@ public interface CRUDRepository extends PagingAndSortingRepository { * @return all elements of the repository type, sorted according to the sort * NOTE: please close the iterable if it is not fully looped through */ - @Transactional EndResult findAll(Sort sort); - /** - * finder that takes the provided sorting and paging into account - * NOTE: the sorting is not yet implemented - * - * @param pageable - * @return all elements of the repository type, sorted according to the sort - * NOTE: please close the iterable if it is not fully looped through - */ - @Transactional - Page findAll(Pageable pageable); - - @Transactional Class getStoredJavaType(Object entity); - @Transactional + EndResult query(String query, Map params); } \ No newline at end of file