繁体   English   中英

Hibernate 2个主键(1是外键)

[英]Hibernate 2 primary keys (1 is a foreign key)

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

成分.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
}

配方.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_id组成: 以某种方式(使用我已经拥有的FK并将recipe_id PK或仅以其他方式获取该 ID 并使其ingredient_id )。

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

ingredient_id(PK)    name      quantity   recipe_id(PK)
1                    Oil       150        1
2                    Salt      5          1
3                    Flour     500        1

知道我该怎么做吗?

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

    }

请务必注意,复合键不能具有 null 值。 因此,只有当您知道每种成分都与食谱绑定时,这才有意义。 在 StackOverflow这个 Hibernate Jira上查看这个答案

您可以查看Hibernate ORM 文档了解更多详细信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM