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

View File

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

View File

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