简体   繁体   中英

Error while using Spring Data JPA mixed with EntityManager in other classes: No bean named 'transactionManager' available

I was working on a newer project from zero, I just use JpaRepository and no extra @Bean , @Qualifier(value = "xxx") or @Configuration are used, thanks to Spring Data JPA.

Now I begin to add code to an old project and I saw classes using EntityManager and @Transactional and typed queries. I think they are not necessary.

So I want to add my new-styled repository while keeping the old code intact, but this class and its test, I see errors:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' available: No matching PlatformTransactionManager bean found for qualifier 'transactionManager' - neither qualifier match nor bean name match!

Repository:

public interface UserPurchaseRepository extends JpaRepository<UserPurchase, Long> {
    /**
     * Find the last purchase time of a user.
     * @param userId the id of user to search
     * @return the last buy time
     */
    OffsetDateTime findLastBuyDateByUserId(String userId);

}

Test:

@RunWith(SpringRunner.class)
@AutoConfigureTestDatabase
@SpringBootTest
public class UserPurchaseServiceIntegrationTest {
    private static final String id = UUID.randomUUID().toString();
    private static final String userId = UUID.randomUUID().toString();
    private static final OffsetDateTime lastBuyDate = OffsetDateTime.now();

    @Autowired
    private UserPurchaseRepository repository;


    @Before
    public void setUp() {
        UserPurchase purchase = new UserPurchase();
        purchase.setId(id);
        purchase.setUserId(userId);
        purchase.setLastBuyDate(lastBuyDate);
        repository.save(purchase);
    }

    @After
    public void cleanUp() {
        repository.deleteAll();
    }

    @Test
    public void testFindLastBuyTimeByUserId() {
        // given (setUp())

        // when
        OffsetDateTime foundLastBuyTime = repository.findLastBuyDateByUserId(userId);

        // then
        Assert.assertEquals(lastBuyDate, foundLastBuyTime);
    }


}

Note that here @Transactional is not needed because it is a find operation. save or delete will be implemented by Spring. So, I think transaction management is not needed here.

Do I have to add a configuration class to tell my repository to use a transaction manager? Is there ways to avoid another class? Why can't it just work without while it worked in another project?

PS: I saw compile("org.springframework.boot:spring-boot-starter-data-redis") and compile("org.springframework.boot:spring-boot-starter-data-jpa") are both listed in build.gradle . Is this relevant?

Well, I found why: we use a library for OpenID named mitre , and this dependency uses EclipseLink as implementation of JPA instead of Hibernate, so we had to exclude hibernate in spring-data-jpa . If we want to use JPA-Hibernate, these two will mix up and @Autowired will easily confuse which to use, so we had to write typed queries only to use this lib. That is why it is like this for now. It seems that I have to write all the queries myself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM