2013-11-03 57 views
1

我正在使用hibernate,並想知道是否有一種方法不要重複值。創建一個約束,不要在Mysql中插入重複的值

我有五列,我和集合這五列的值不要重複例如。

 
column 1 | column 2 | column 3 | column 4 | column 5 
apple  fruit  red   good  healthy 

如果有人嘗試插入同一行的值不會保存,但如果一個列有值改變像

 
column 1 | column 2 | column 3 | column 4 | column 5 
strawberry fruit  red   good  healthy 

那麼,應該怎麼辦?

回答

1

你需要一個複合唯一約束:

alter table yourtable add unique index(column1, column2, column3, column4, column5); 
+0

外觀極好,我想要的方式。 –

+0

如果您使用Hibernate的DDL模式生成,您還可以使用@Table註釋的uniqueConstraints屬性讓Hibernate爲您創建此唯一索引。 –

相關問題