ahrooran
ahrooran

Reputation: 1115

Does user specified methods in JpaRepository gets executed in single transaction?

I have custom methods in JpaRepository and they are called recursively. firstMethod is actually calling data from DB. secondMethod and thirdMethod are calling firstMethod and secondMethod respectively.

Does thirdMethod executes in a single transaction? i.e., I want entire query list to be fired in a single go, fetch entire list from db and return in a single go instead of multiple queries fired per object in list.

public interface SomeDao extends JpaRepository<SomeObject, SomeObjectId> {

    @Deprecated
    @Query("custom query")
    Slice<SomeObject> firstMethod(BigInteger a, Timestamp b, Pageable c);

    default Optional<SomeOtherObject> secondMethod(SomeObject someObject) {
        // calls first method
        // object has separate fields that apply in first method -> someObject.a, someObject.b etc
    }

    // I want to know whether third method gets executed in single transaction???
    default List<Optional<SomeOtherObject>> thirdMethod(List<SomeObject> listOfObjects) {
        // calls second method for a list of objects
    }
}

Upvotes: 0

Views: 23

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 81930

It seems you are confusing transactions and query execution.

If everything runs in a single transaction depends on the @Transactional annotation you put or don't put on your repository method.

What query gets executed solely depends on firstMethod and it's query annotation. It will get executed for each invocation of the method.

Upvotes: 1

Related Questions