Java 如何保存包含多对多关系的多对多对象

Java 如何保存包含多对多关系的多对多对象,java,spring,jpa,Java,Spring,Jpa,我有一个使用JAVA与spring和JPA的bit项目。我的实体相当复杂,它们如下: 用户实体: private @NonNull @Id String email; private String fullName; private String gender; @OneToMany(mappedBy = "user",fetch = FetchType.LAZY) private Set<UserIngredient> userIngredients; 互惠

我有一个使用JAVA与spring和JPA的bit项目。我的实体相当复杂,它们如下:

用户实体:

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

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

@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;
月经量:

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

private Date timestamp;
private short numOfErrors;

@OneToMany(mappedBy = "recipe", fetch = FetchType.LAZY)
private Set<MenuRecipe> menuRecipes;
现在,我的问题是,我试图通过以下操作创建菜单:

// 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()));
    }
//获取所有用户禁用的成分
List uiArr=userIngrdientsDAL
.findAllByUser_EmailAndType(userEmail,IngredientTypeEnum.Forbidded.name(),PageRequest.of(0,1000))
.stream().map(ui->ui.getComponent())
.collect(Collectors.toList());
//获得所有合适的食谱
列出RecipeEntityes=recipeDAL.FindDintintByRecipeComponents\u IngredientNotIn(uiArr);
Set recipies=new HashSet();
menu.setMenuRecipes(Recipes);
对于(int i=0;i
我在添加行上感觉到SELECT查询溢出。 有什么想法吗

@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;
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id private Long id;

private Date timestamp;
private short numOfErrors;

@OneToMany(mappedBy = "recipe", fetch = FetchType.LAZY)
private Set<MenuRecipe> menuRecipes;
@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;
// 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()));
    }