== Трансакции Употребата на трансакции беше од голема корист за операции кои требаа да се извршат или сите или да падне, за лесно да се најде грешката. Во мојата апликација истото го употребив во неколку наврати, кои ќе ги покажам подолу. === 1. Трансакција при регистрација на нов корисник {{{ @Transactional public void register_user(String name, String surname, String email, String password, String password_confirmation) { if (email == null || email.isEmpty() || password == null || password.isEmpty() || password_confirmation == null || password_confirmation.isEmpty()) { throw new NoExistingCredentialsException("Credentials cannot be empty."); } if (!password.equals(password_confirmation)) { throw new IllegalArgumentException("Passwords do not match. Check the mistake and try again."); } if (reportiumUserRepository.findByEmail(email).isPresent()) { throw new UserAlreadyExistsException(String.format("User with this email '%s' already exists.", email)); } //creating the user ReportiumUser new_application_user = new ReportiumUser(); new_application_user.setActive(true); new_application_user.setName(name); new_application_user.setSurname(surname); new_application_user.setEmail(email); new_application_user.setPasswordHash(passwordEncoder.encode(password)); new_application_user.setCreatedAt(LocalDateTime.now()); ReportiumUser savedUser = reportiumUserRepository.save(new_application_user); //I have a trigger that creates the user profile UserProfile user_profile = userProfileRepository.findByReportiumUser(savedUser).get(); UserProfileLog userProfileLog = new UserProfileLog(); userProfileLog.setUserProfile(user_profile); userProfileLog.setChangedAt(LocalDateTime.now()); String description = String.format("New user <%s %s> with mail '%s' has been registered.", savedUser.getName(), savedUser.getSurname(), savedUser.getEmail()); userProfileLog.setChangeDescription(description); profileLogRepository.save(userProfileLog); } }}} === 2. Креирање на Log запис во табелата {{{ @Override @Transactional public void createLog(Integer userId, LogType type) { if (type == null) { throw new IllegalArgumentException("Log type must not be null"); } UserProfile profile = profileRepository .findById(userId).orElseThrow(() -> new NoExistingCredentialsException("User does not exist")); String fullName = buildFullName(profile); String description = switch (type) { case LOGIN -> "User %s logged in successfully.".formatted(fullName); case REGISTRATION -> "User %s registered successfully.".formatted(fullName); case LOGOUT -> "User %s logged out successfully.".formatted(fullName); case CHANGE_ROLE -> "The role of user %s was changed successfully.".formatted(fullName); case CHANGE_PASSWORD -> "User %s changed the password successfully.".formatted(fullName); }; UserProfileLog log = new UserProfileLog(); log.setUserProfile(profile); log.setChangeDescription(description); log.setChangedAt(LocalDateTime.now()); userProfileLogRepository.save(log); } }}} === 3.