Add transactional support to CharacterService, ScenarioService, and UserService

- Annotate create, update, and delete methods in CharacterService and ScenarioService with @Transactional to ensure proper transaction management.
- Add @Transactional annotation to the createUser method in UserService for consistency in transaction handling across services.
- Enhance data integrity and consistency during operations involving character, scenario, and user management.
This commit is contained in:
Konrad Neitzel 2026-02-22 12:22:41 +01:00
parent 2c61ab5fc9
commit 1e1368e519
3 changed files with 10 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import de.neitzel.roleplay.fascade.model.CreateCharacterRequest;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -54,6 +55,7 @@ public class CharacterService {
* @param request the create request (name and role required) * @param request the create request (name and role required)
* @return the created character definition * @return the created character definition
*/ */
@Transactional
public CharacterDefinition create(final CreateCharacterRequest request) { public CharacterDefinition create(final CreateCharacterRequest request) {
CharacterEntity entity = fromRequest(request, request.getId() != null ? request.getId() : UUID.randomUUID()); CharacterEntity entity = fromRequest(request, request.getId() != null ? request.getId() : UUID.randomUUID());
characterRepository.persist(entity); characterRepository.persist(entity);
@ -68,6 +70,7 @@ public class CharacterService {
* @return the updated character definition * @return the updated character definition
* @throws java.util.NoSuchElementException if the character does not exist * @throws java.util.NoSuchElementException if the character does not exist
*/ */
@Transactional
public CharacterDefinition update(final UUID id, final CreateCharacterRequest request) { public CharacterDefinition update(final UUID id, final CreateCharacterRequest request) {
CharacterEntity entity = characterRepository.findByIdOptional(id); CharacterEntity entity = characterRepository.findByIdOptional(id);
if (entity == null) { if (entity == null) {
@ -84,6 +87,7 @@ public class CharacterService {
* @param id the character UUID * @param id the character UUID
* @return true if deleted, false if no character existed * @return true if deleted, false if no character existed
*/ */
@Transactional
public boolean delete(final UUID id) { public boolean delete(final UUID id) {
CharacterEntity entity = characterRepository.findByIdOptional(id); CharacterEntity entity = characterRepository.findByIdOptional(id);
if (entity == null) { if (entity == null) {

View File

@ -13,8 +13,8 @@ import de.neitzel.roleplay.fascade.model.ScenarioSummary;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@ -66,6 +66,7 @@ public class ScenarioService {
* @return the created scenario summary * @return the created scenario summary
* @throws IllegalArgumentException if any referenced character id is not found * @throws IllegalArgumentException if any referenced character id is not found
*/ */
@Transactional
public ScenarioSummary create(final CreateScenarioRequest request) { public ScenarioSummary create(final CreateScenarioRequest request) {
UUID scenarioId = UUID.randomUUID(); UUID scenarioId = UUID.randomUUID();
ScenarioEntity scenario = new ScenarioEntity(); ScenarioEntity scenario = new ScenarioEntity();
@ -104,6 +105,7 @@ public class ScenarioService {
* @throws java.util.NoSuchElementException if the scenario does not exist * @throws java.util.NoSuchElementException if the scenario does not exist
* @throws IllegalArgumentException if any referenced character id is not found * @throws IllegalArgumentException if any referenced character id is not found
*/ */
@Transactional
public ScenarioSummary update(final UUID id, final CreateScenarioRequest request) { public ScenarioSummary update(final UUID id, final CreateScenarioRequest request) {
ScenarioEntity scenario = scenarioRepository.findByIdWithCharacters(id); ScenarioEntity scenario = scenarioRepository.findByIdWithCharacters(id);
if (scenario == null) { if (scenario == null) {
@ -141,6 +143,7 @@ public class ScenarioService {
* @param id the scenario UUID * @param id the scenario UUID
* @return true if deleted, false if no scenario existed * @return true if deleted, false if no scenario existed
*/ */
@Transactional
public boolean delete(final UUID id) { public boolean delete(final UUID id) {
ScenarioEntity scenario = scenarioRepository.findByIdWithCharacters(id); ScenarioEntity scenario = scenarioRepository.findByIdWithCharacters(id);
if (scenario == null) { if (scenario == null) {

View File

@ -7,6 +7,7 @@ import de.neitzel.roleplay.data.UserRepository;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -42,6 +43,7 @@ public class UserService {
* @return the created user summary * @return the created user summary
* @throws IllegalArgumentException if username is blank, password is blank, role is invalid, or username already exists * @throws IllegalArgumentException if username is blank, password is blank, role is invalid, or username already exists
*/ */
@Transactional
public UserSummary createUser(final CreateUserRequest request) { public UserSummary createUser(final CreateUserRequest request) {
String username = request.getUsername(); String username = request.getUsername();
String password = request.getPassword(); String password = request.getPassword();