JPA 中未创建唯一约束

发布于 2024-09-03 13:44:37 字数 520 浏览 15 评论 0原文

我创建了以下实体 bean,并指定两列是唯一的。现在我的问题是创建的表没有唯一约束,并且日志中没有错误。 有人有想法吗?

@Entity
@Table(name = "cm_blockList", uniqueConstraints = @UniqueConstraint(columnNames = {"terminal", "blockType"}))
public class BlockList {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name="terminal")
    private Terminal terminal;
    @Enumerated(EnumType.STRING)
    private BlockType blockType;
    private String regEx;
}

I have created the following entity bean, and specified two columns as being unique. Now my problem is that the table is created without the unique constraint, and no errors in the log.
Does anyone have an idea?

@Entity
@Table(name = "cm_blockList", uniqueConstraints = @UniqueConstraint(columnNames = {"terminal", "blockType"}))
public class BlockList {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name="terminal")
    private Terminal terminal;
    @Enumerated(EnumType.STRING)
    private BlockType blockType;
    private String regEx;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

作业与我同在 2024-09-10 13:44:37

好吧,我找到了另一种方式来进行设计。更多的是因为设计的演变而不是解决方法。
然而,我从一位同事那里听说,他也有同样的问题,只有在创建整个数据库时,唯一约束才由 hibernate (我们正在运行 JBoss 4.3)创建。当您在现有数据库中创建新表时,它将不起作用。
因此,在 persistence.xml 中,需要将 hibernate.hbm2ddl.auto 设置为 create-drop 才能使其正常工作。但我无法证实这一点。

Well, I found another way to make the design. More because the design evolved than a work around.
I heard however from a colleague, that had had the same problem, that unique constraint only are created by hibernate (we are running JBoss 4.3) when the entire database is created. It will not work when you create a new table in an existing database.
So in persistence.xml it is necessary to set hibernate.hbm2ddl.auto to create-drop to make it work. I can not confirm this though.

彡翼 2024-09-10 13:44:37

您不必删除所有内容,只需删除要为其创建唯一索引的特定表即可

You dont have to drop everything, just drop this particular table you want to create unique index for

爱殇璃 2024-09-10 13:44:37

更多建议而不是真正的答案...

  • 您应该澄清您正在使用哪个 JPA 提供程序(和版本)来解决此类问题。

  • 我注意到您的其中一个列是 ManyToOne 关联的一部分,这可能是您的 JPA 提供商未能很好处理的“特殊”情况。我所说的处理不好,是指错误。我会检查问题跟踪器中是否存在现有问题(也许会创建一个新问题,您的案例似乎符合 JPA 标准)。

More suggestions than a real answer...

  • you should clarify which JPA provider (and the version) you are using for such questions.

  • I notice that one of your column is part of a ManyToOne association, this might be a "special" case not well handled by your JPA provider. By not well handled, I mean bug. I'd check the issue tracker for an existing issue (and maybe create a new one, your case seems JPA compliant).

歌枕肩 2024-09-10 13:44:37

如果您想要唯一约束,您应该通过sql来完成:

ALTER TABLE cm_blockList ADD CONSTRAINT cm_blockList_unq UNIQUE (terminal);

另请参阅:https://stackoverflow.com/a/3498242/1244488

If you want the unique constraint, you should do it by sql:

ALTER TABLE cm_blockList ADD CONSTRAINT cm_blockList_unq UNIQUE (terminal);

See also: https://stackoverflow.com/a/3498242/1244488

惟欲睡 2024-09-10 13:44:37

我也遇到了同样的问题,但在映射文件中添加 unique-key="UK1_test" 效果很好。

<属性名称=“hibernate.hbm2ddl.auto”>创建
在创建的映射文件中使用 unique-key="UK1_test" unique="true"
更改表员工添加约束UK1_test unique(addrId)

I also faced the same issue, but adding unique-key="UK1_test" in the mapping file worked fine.

<property name="hibernate.hbm2ddl.auto">create</property>
with unique-key="UK1_test" unique="true" in the mapping file created
alter table employee add constraint UK1_test unique (addrId)

清眉祭 2024-09-10 13:44:37

这些天,当我添加 @Column(nullable = false, unique = true, length = 16) 并且没有获得唯一索引时,我遇到了同样的问题。

经过几次尝试后,我发现 spring data jpa 在字段更改时更新数据库表(ddl),但不随 @Column 更改而更改。所以创建表后分配unique = true是没有用的。要获得具有唯一索引的表,您需要删除旧的表。然后重新运行代码,让jpa再次创建表。

我相信它是因为 jpa 在避免新的唯一索引时不知道旧表中是否包含相同的字段值。因此它接受一个全新的表。

These days I meet the same problem when I add @Column(nullable = false, unique = true, length = 16) And not got a unique index.

After a few try with it, I found spring data jpa update database table(ddl) when fields change but not with the @Column changes. So assign unique = true after tables created is useless.To get a table with unique index,You need to drop the old one.Then re-run the code, let jpa create table again.

I believe it it because jpa doesn't know whether it contains same field value in old table when avoiding new unique index.So it accept a brand new table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文