未在 JPA 中创建的唯一约束

Unique constraint not created in JPA(未在 JPA 中创建的唯一约束)
本文介绍了未在 JPA 中创建的唯一约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
}

推荐答案

好吧,我找到了另一种进行设计的方法.更多是因为设计的演变而不是解决方法.
然而,我从一位同事那里听说,他遇到了同样的问题,唯一约束仅在创建整个数据库时由休眠(我们正在运行 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.

这篇关于未在 JPA 中创建的唯一约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)