From ba74ee10ba2263f58899c50dee16ed1ebe388e3d Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 19 Jun 2013 09:35:18 +0200 Subject: [PATCH] =?UTF-8?q?DATAJPA-359=20-=20Correctly=20apply=20lock=20co?= =?UTF-8?q?nfig=20for=20findOne(=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SImpleJpaRepository.findOne(…) now applies the lock configuration potentially set on a redeclared method of the repository interface. --- .../support/SimpleJpaRepository.java | 11 ++-- .../jpa/repository/LockIntegrationTests.java | 56 ++++++++++++------- .../jpa/repository/sample/RoleRepository.java | 9 ++- 3 files changed, 49 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index 2fbfd7f17..b4bf2de8f 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -198,15 +198,16 @@ public class SimpleJpaRepository implements JpaRepos /* * (non-Javadoc) - * - * @see - * org.springframework.data.repository.Repository#readById(java.io.Serializable - * ) + * @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable) */ public T findOne(ID id) { Assert.notNull(id, "The given id must not be null!"); - return em.find(getDomainClass(), id); + + LockModeType type = lockMetadataProvider == null ? null : lockMetadataProvider.getLockModeType(); + Class domainType = getDomainClass(); + + return type == null ? em.find(domainType, id) : em.find(domainType, id, type); } /* diff --git a/src/test/java/org/springframework/data/jpa/repository/LockIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/LockIntegrationTests.java index 3ba012ae4..47e094c71 100644 --- a/src/test/java/org/springframework/data/jpa/repository/LockIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/LockIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * Copyright 2011-2013 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. @@ -26,6 +26,7 @@ import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -43,28 +44,18 @@ import org.springframework.data.jpa.repository.support.JpaRepositoryFactory; @RunWith(MockitoJUnitRunner.class) public class LockIntegrationTests { - @Mock - EntityManager em; - @Mock - CriteriaBuilder builder; - @Mock - CriteriaQuery criteriaQuery; - @Mock - JpaEntityInformation information; - @Mock - TypedQuery query; + @Mock EntityManager em; + @Mock CriteriaBuilder builder; + @Mock CriteriaQuery criteriaQuery; + @Mock JpaEntityInformation information; + @Mock TypedQuery query; - /** - * @see DATAJPA-73 - */ - @Test - public void usesLockInformationAnnotatedAtRedeclaredMethod() { + RoleRepository repository; + + @Before + public void setUp() { when(information.getJavaType()).thenReturn(Role.class); - when(em.getCriteriaBuilder()).thenReturn(builder); - when(builder.createQuery(Role.class)).thenReturn(criteriaQuery); - when(em.createQuery(criteriaQuery)).thenReturn(query); - when(query.setLockMode(any(LockModeType.class))).thenReturn(query); JpaRepositoryFactory factory = new JpaRepositoryFactory(em) { @Override @@ -74,10 +65,33 @@ public class LockIntegrationTests { } }; - RoleRepository repository = factory.getRepository(RoleRepository.class); + repository = factory.getRepository(RoleRepository.class); + } + + /** + * @see DATAJPA-73 + */ + @Test + public void usesLockInformationAnnotatedAtRedeclaredMethod() { + + when(em.getCriteriaBuilder()).thenReturn(builder); + when(builder.createQuery(Role.class)).thenReturn(criteriaQuery); + when(em.createQuery(criteriaQuery)).thenReturn(query); + when(query.setLockMode(any(LockModeType.class))).thenReturn(query); repository.findAll(); verify(query).setLockMode(LockModeType.READ); } + + /** + * @see DATAJPA-359 + */ + @Test + public void usesLockInformationAnnotatedAtRedeclaredFindOne() { + + repository.findOne(1); + + verify(em).find(Role.class, 1, LockModeType.READ); + } } diff --git a/src/test/java/org/springframework/data/jpa/repository/sample/RoleRepository.java b/src/test/java/org/springframework/data/jpa/repository/sample/RoleRepository.java index 07a82281b..81cfa1337 100644 --- a/src/test/java/org/springframework/data/jpa/repository/sample/RoleRepository.java +++ b/src/test/java/org/springframework/data/jpa/repository/sample/RoleRepository.java @@ -33,5 +33,12 @@ public interface RoleRepository extends CrudRepository { * @see org.springframework.data.repository.CrudRepository#findAll() */ @Lock(LockModeType.READ) - public Iterable findAll(); + Iterable findAll(); + + /* + * (non-Javadoc) + * @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable) + */ + @Lock(LockModeType.READ) + Role findOne(Integer id); }