Add a unique constraint over muliple reference columns

陌路散爱 提交于 2019-12-25 04:37:13

问题


Hello i want to add a unique constraint over two referenced columns:

@Entity()
@Table(uniqueConstraints = @UniqueConstraint(columnNames = { "ast", "tfs" }))
public class N extends UUID {

@ManyToOne(optional = false)
@JoinColumn(name = "ast")
private A ast;

@ManyToOne(optional = false)
@JoinColumn(name = "tfs")
private T tfs;

In Class UUID is logic for generating a Primary ID.

Hibernate generates 3 Constraint, PK, 2 Secondary Keys.. but i am expecting 4 Constraints. Does someone have a idea ?

Edit:

@Entity
public class A extends UUID implements Serializable {

@OneToMany(mappedBy = "ast")
private List<N> nutz;

and

@Entity
public class T extends UUID implements Serializable {

@OneToMany(mappedBy = "tfs")
private List<N> nutz;

回答1:


It works if I set:

<property name="hibernate.hbm2ddl.auto" value="create-drop" />

instead of

<property name="hibernate.hbm2ddl.auto" value="update" />

It seems Hibernate only creates constaints if the whole database is created and not when it creates the table! Confusing. related



来源:https://stackoverflow.com/questions/20194314/add-a-unique-constraint-over-muliple-reference-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!