How to save many to many object contains many to many relationship

Viewed 59

I have a bit project using JAVA with spring and JPA. My entities are quite complicated, here are they:

UserEntity:

private @NonNull @Id String email;
private String fullName;
private String gender;

@OneToMany(mappedBy = "user",fetch = FetchType.LAZY)
private Set<UserIngredient> userIngredients;

IngredientEntity:

@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id private Long id;
@Column(nullable =false)
private String name;
private String ingredientGroup;

@OneToMany(mappedBy = "ingredient",fetch = FetchType.LAZY)
private Set<UserIngredient> userIngredients;
@OneToMany(mappedBy = "recipe",fetch = FetchType.LAZY)
 private Set<RecipeIngredient> recipeIngredients;

UserIngredient:

@EmbeddedId
private UserIngredientKey id;

@ManyToOne(fetch = FetchType.LAZY)
@MapsId("userEmail")
@JoinColumn(name = "user_email")
private UserEntity user;

@ManyToOne(fetch = FetchType.LAZY)
@MapsId("ingredientId")
@JoinColumn(name = "ingredient_id")
private IngredientEntity ingredient;

private String type;

RecipeEntity:

@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private long recipeId;
@Column(unique = true)
private @NonNull String name;
private String prepartion;
private String createdBy;

@OneToMany(mappedBy = "recipe", fetch = FetchType.LAZY)
private Set<RecipeIngredient> recipeIngredients;

@OneToMany(mappedBy = "menu", fetch = FetchType.LAZY)
private Set<MenuRecipe> menuRecipe;

RecipeIngredient:

@EmbeddedId
private RecipeIngredientId id;

@ManyToOne(fetch = FetchType.LAZY)
@MapsId("recipeId")
@JoinColumn(name = "recipe_id")
private RecipeEntity recipe;

@ManyToOne(fetch = FetchType.LAZY)
@MapsId("ingredientId")
@JoinColumn(name = "ingredient_id")
private IngredientEntity ingredient;

private double amount;

MenuEntity:

@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id private Long id;

private Date timestamp;
private short numOfErrors;

@OneToMany(mappedBy = "recipe", fetch = FetchType.LAZY)
private Set<MenuRecipe> menuRecipes;

MenuRecipe:

@EmbeddedId
private MenuRecipeId id;

@ManyToOne(fetch = FetchType.LAZY)
@MapsId("recipeId")
@JoinColumn(name = "recipe_id")
private RecipeEntity recipe;

@ManyToOne(fetch = FetchType.LAZY)
@MapsId("menuId")
@JoinColumn(name = "menu_id")
private MenuEntity menu;

private Boolean match;

Now, my problem is that I try to create menu by doing:

// get All user FORBIDDEN ingredients
    List<IngredientEntity> uiArr = userIngrdientsDAL
            .findAllByUser_EmailAndType(userEmail, IngredientTypeEnum.FORBIDDEN.name(), PageRequest.of(0, 1000))
            .stream().map(ui -> ui.getIngredient())         
            .collect(Collectors.toList());
    // get all appropriate recipes
    List<RecipeEntity> recipeEnityties = recipeDAL.findDistinctByRecipeIngredients_IngredientNotIn(uiArr);
    
    Set<MenuRecipe> recipies = new HashSet<>();
    menu.setMenuRecipes(recipies);
    for (int i = 0; i < days; i++) {
        RecipeEntity recipe =recipeEnityties.remove(new Random().nextInt(recipeEnityties.size())); 
        recipies.add(createMenuRecipe(menu.getId(), recipe.getRecipeId()));
    }

And I felt on the add line with overflow of SELECT queries. Any ideas?

0 Answers
Related