【问题标题】:@UniqueConstraints not working on @JoinTable@UniqueConstraints 不适用于 @JoinTable
【发布时间】:2012-01-23 14:16:02
【问题描述】:

我正在使用 Playframework 1.2.4 和 PostgresSQL 9.1.2。我有以下实体:Recipe 和 RecipeItem。一个Recipe 有一组RecipeItems。我在 Recipe 类中对一组配方项进行了如下注释:

@Required
@MinSize(1)
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable( name = "RecipeItemForIngredients",
joinColumns = @JoinColumn(name = "recipeId"),
inverseJoinColumns = @JoinColumn(name = "recipeItemId"),
uniqueConstraints = @UniqueConstraint(name = "uq_recipeItemPerRecipe",
columnNames = {"recipeId", "recipeItemId"}))
public Set<RecipeItem> items = Sets.newHashSet();

但是当我检查 PgAdmin 以查看约束是否已应用于 RecipeItemForIngredients 表时,我找不到它。这就是 PgAdmin 显示的内容。

CREATE TABLE recipeitemforingredients
(
  recipeid bigint NOT NULL,
  recipeitemid bigint NOT NULL,
  CONSTRAINT recipeitemforingredients_pkey PRIMARY KEY (recipeid, recipeitemid),
  CONSTRAINT fk5ac547a883708db FOREIGN KEY (recipeid)
      REFERENCES recipe (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk5ac547ad6e1da8f FOREIGN KEY (recipeitemid)
      REFERENCES "recipe$recipeitem" (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT recipeitemforingredients_recipeitemid_key UNIQUE (recipeitemid)
)

有谁知道为什么会发生这种情况?也许 Playframework 使用的 ORM 不支持这个注解。

【问题讨论】:

    标签: jpa orm playframework


    【解决方案1】:

    这个约束没有多大意义,而且默认情况下你有一个更强的约束:

    CONSTRAINT recipeitemforingredients_recipeitemid_key UNIQUE (recipeitemid)
    

    如果recipeItemId是唯一的,那么元组(recipeId, recipeItemId)当然也是唯一的。

    另外,由于表的PK是(recipeId, recipeItemId),所以该约束已经被PK约束应用了。

    【讨论】:

    • 好的,但由于主键,配方不能重复成分。我想知道为什么它决定使用主键而不是只使用唯一的命名约束
    • 我不知道。也许它足够聪明,可以检测到重复或不必要的约束。
    猜你喜欢
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多