Hibernate 2个主键(1个是外键)

时间:2021-03-26 14:39:59

标签: java mysql spring-boot hibernate jpa

我的问题是关于下面的代码:

Ingredient.java

@Entity
@Table(name = "recipes_ingredients")
public class Ingredient implements Serializable {
 
    @Id
    @Column(name = "ingredient_id")
    private Long id;
 
    @NotEmpty
    private String name;
 
    private Integer quantity;
 
    // Getters and Setters
}

Recipe.java

@Entity
@Table(name = "recipes")
public class Recipe implements Serializable {
 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "recipe_id")
    private Long id;
 
    @NotEmpty
    private String name;
 
    @ManyToOne(fetch = FetchType.LAZY)
    private Client client;
 
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "fk_recipe_id")
    private List<Ingredient> ingredients;
 
    // Getters and Setters
}

我想要做的是 PKrecipes_ingredients 表)以某种方式由:ingredient_idrecipe_id 组成(使用我已经拥有的 FK 并使其也PK 或者只是以其他方式获取该 ID 并使用 {{1} 使其PK }).

ingredient_id 将是每个食谱从 1 开始的数字,这样如果我有一个 recipe_id = 1 和 3 成分的食谱,我的想法是在成分表中它有类似的东西这个:

ingredient_id

知道我该怎么做吗?

1 个答案:

答案 0 :(得分:0)

    public class IngredientId {

        private Recipe recipe;

        private Long id;

        public IngredientId(Recipe recipe, Long id) {
            this.recipe = recipe;
            this.id = id;
        }

        public IngredientId() {
        }

        //Getters and setters are omitted for brevity

        public Recipe getRecipe() {
            return recipe;
        }

        public void setRecipe(Recipe recipe) {
            this.recipe = recipe;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        @Override
        public boolean equals(Object o) {
            if ( this == o ) {
                return true;
            }
            if ( o == null || getClass() != o.getClass() ) {
                return false;
            }
            IngredientId ingredientId = (IngredientId) o;
            return Objects.equals( recipe, ingredientId.recipe ) && Objects.equals( id, ingredientId.id );
        }

        @Override
        public int hashCode() {
            return Objects.hash( recipe, id );
        }
    }

    @Entity
    @IdClass( IngredientId.class )
    @Table(name = "recipes_ingredients")
    public class Ingredient {

        @Id
        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "recipe_id")
        private Recipe recipe;

        @Id
        @GeneratedValue
        @Column(name = "ingredient_id")
        private Long id;

        private String name;

        private Integer quantity;

        public IngredientId getId() {
            return new IngredientId( recipe, id );
        }

        public void setId(IngredientId id) {
            this.recipe = id.getRecipe();
            this.id = id.getId();
        }
    }

    @Entity
    @Table(name = "recipes")
    public class Recipe {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "recipe_id")
        private Long id;

        private String name;

        @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
        @JoinColumn(name = "recipe_id")
        private List<Ingredient> ingredients = new ArrayList<>();

    }

需要注意的是,复合键不能有空值。 因此,只有当您知道每种成分都与食谱相关联时,这才有意义。请参阅 this answer on StackOverflowthis Hibernate Jira

您可以查看 Hibernate ORM documentation 了解更多详情。

相关问题