@UniqueConstraints没有在@JoinTable上工作

时间:2012-01-23 14:16:03

标签: jpa orm playframework

我正在使用Playframework 1.2.4和PostgresSQL 9.1.2。我有以下实体:Recipe和RecipeItem。食谱有一套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可能不支持此注释。

1 个答案:

答案 0 :(得分:1)

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

CONSTRAINT recipeitemforingredients_recipeitemid_key UNIQUE (recipeitemid)

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

此外,由于表的PK是(recipeId, recipeItemId),因此约束已经被PK约束应用。