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

m3eecexj  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(263)

我有一个使用java和spring和jpa的小项目。我的实体非常复杂,它们是:
用户实体:

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 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;

用户成分:

@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;

相互关系:

@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;

收件人:

@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()));
    }

我在add行上感觉到select查询溢出。有什么想法吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题