TransWikia.com

PersistentObjectException: detached entity passed to persist [JpaRepository]

Stack Overflow Asked by Adriano on January 4, 2021

I’m using Spring Data JPA with JpaRepository and I couldn’t find an answer to why my entity is detached and I can’t save with children entity.

I want to save Recipe but first I need to save Ingredient in another service. Recipe and Ingredient is connected many to many relations by RecipeIngredients object.

@Transactional
public RecipeDto updateRecipe(UserPrincipal userPrincipal, String recipeName, RecipeDto recipeDto) {
            Recipe recipeToEdit = recipeRepository.findByName(recipeName).orElseThrow(EntityNotFoundException::new);
            recipeToEdit.setIngredients(recipeIngredientsService.refillRecipeIngredientsList(recipeDto.getIngredients(), recipeToEdit));
}

Table relation

@Entity
public class RecipeIngredients {

    @EmbeddedId
    private RecipeIngredientsId recipeIngredientsId;

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("recipeId")
    @ToString.Exclude
    private Recipe recipe;

    @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE})
    @MapsId("ingredientId")
    @ToString.Exclude
    private Ingredient ingredient;
}

@Entity
@Getter
@Setter
public class Ingredient {

@Id
    @Column(name = "id", updatable = false)
    private Long id;
    @OneToMany(mappedBy = "ingredient", cascade = CascadeType.ALL, orphanRemoval = true)
    @ToString.Exclude
    private List<RecipeIngredients> recipeIngredients;

    public void addRecipeIngredient(RecipeIngredients recipeIngredient) {
        if(recipeIngredients == null) {
            recipeIngredients = new ArrayList<>();
        }
        recipeIngredients.add(recipeIngredient);
        recipeIngredient.setIngredient(this);
    }
}

Before I want to save Ingredient entity I want to check there is exist so I pull it from external service then map It to DTO object. If there is no Ingredient I want to save it and get his object, which will be connected children RecipeIngredients and this is connected to Recipe so when I’m saving recipe it should save me RecipeIngredient with it of Ingredient.

public void refillRecipeIngredientsList(List<RecipeIngredientsDto> recipeIngredientsDtos, Recipe recipe) {
    removeOldIngredientsIfExist(recipe);
    if (recipeIngredientsDtos != null) {
        for (RecipeIngredientsDto recipeIngredientsDto : recipeIngredientsDtos) {
            IngredientDto ingredient = pullSavedIngredient(recipeIngredientsDto);
            RecipeIngredients recipeIngredient = this.recipeIngredientsDtoToEntityMapper.recipeIngredientsToEntity(recipeIngredientsDto, recipe, ingredientDto);
            recipe.addIngredient(recipeIngredient);
            ingredient.addRecipeIngredient(recipeIngredient);
        }
    }
}

But in every way I received org.hibernate.PersistentObjectException: detached entity passed to persist: com.app.todaysdinner.entity.ingredient.Ingredient. Do anyone know the answer how to reattach Ingredient entity?

[EDIT]

I found out that when I want to update through Recipe cascade on RecipeIngredients also cascade onto Ingredients that means it invoke PERSISTANCE method even if allowed is only cascade = {CascadeType.MERGE}. But I can’t find answer why in one @Transaction object is being detached.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP