Skip to content

Instantly share code, notes, and snippets.

@brfrn169
Created January 25, 2023 01:19
Show Gist options
  • Save brfrn169/b050a161914cff6927959ca147551840 to your computer and use it in GitHub Desktop.
Save brfrn169/b050a161914cff6927959ca147551840 to your computer and use it in GitHub Desktop.
Examples of Spring Data JDBC integration
@Repository
public interface NorthAccountRepository
extends PagingAndSortingRepository<NorthAccount, Integer>,
ScalarDbHelperRepository<NorthAccount> {
@Transactional
default void transferToSouthAccount(
@Nonnull SouthAccountRepository southAccountRepository,
int fromId, int toId, int value) {
NorthAccount fromEntity =
findById(fromId).orElseThrow(() -> new AssertionError("Not found: " + fromId));
SouthAccount toEntity =
southAccountRepository
.findById(toId)
.orElseThrow(() -> new AssertionError("Not found: " + toId));
update(new NorthAccount(fromEntity.id, fromEntity.balance - value));
southAccountRepository.update(new SouthAccount(toEntity.id, toEntity.balance + value));
}
@Transactional
default void deleteAfterSelect(int id) {
findById(id).ifPresent(this::delete);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment