Home > Back-end >  Extra association table is created in spring boot
Extra association table is created in spring boot

Time:10-15

I'm currently working on developing a recipe application and I'm having trouble with DB table generation.

Here are the Entity files I'm using:

// Recipe.java
@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "recipes")
public class Recipe {
    @Id
    @GeneratedValue
    private int id;
    private String name;
    private String description;
    private String instruction;

    @ManyToOne
    private User user;

    @OneToMany(cascade=CascadeType.ALL)
    private List<RecipeIngredient> ingredients = new ArrayList<>();
}

// Ingredient.java
@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "ingredients")
public class Ingredient {
    @Id
    @GeneratedValue
    private int id;
    private String name;
}

// RecipeIngredient.java
@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class RecipeIngredient {
    @Id
    @GeneratedValue
    private int id;

    @ManyToOne
    private Ingredient ingredient;

    private String amount;
}

enter image description here

Spring Boot Automatically creates tables for me but I just wanna have one table for RecipeIngredient, but it creates two tables for them. It works perfectly fine but the thing I want is just how to make these two tables into one or make spring boot not generate one of them.

CodePudding user response:

If you want recipe_ingedients table only delete recipeIngredient Entity Class and if you want to keep recipe_ingredient table remove this:

@OneToMany(cascade=CascadeType.ALL)
private List<RecipeIngredient> ingredients = new ArrayList<>();
  • Related