Java中的@UniqueConstraint注解
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3126769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@UniqueConstraint annotation in Java
提问by xyz
I have a Java bean. Now, I want to be sure that the field should be unique.
我有一个 Java bean。现在,我想确保该字段应该是唯一的。
I am using the following code:
我正在使用以下代码:
@UniqueConstraint(columnNames={"username"})
public String username;
But I'm getting some error:
但我收到了一些错误:
@UniqueConstraint is dissallowed for this location
What's the proper way to use unique constraints?
使用唯一约束的正确方法是什么?
Note:I am using play framework.
注意:我正在使用播放框架。
回答by mdma
To ensure a field value is unique you can write
为了确保字段值是唯一的,您可以编写
@Column(unique=true)
String username;
The @UniqueConstraint annotation is for annotating multiple unique keys at the table level, which is why you get an error when applying it to a field.
@UniqueConstraint 注释用于在表级别注释多个唯一键,这就是将其应用于字段时出错的原因。
References (JPA TopLink):
参考文献(JPA TopLink):
回答by Divanshu
You can use at class level with following syntax
您可以使用以下语法在类级别使用
@Entity
@Table(uniqueConstraints={@UniqueConstraint(columnNames={"username"})})
public class SomeEntity {
@Column(name = "username")
public String username;
}
回答by FrancescoM
I'm currently using play framework too with hibernate and JPA 2.0 annotation and this model works without problems
我目前也在使用带有 hibernate 和 JPA 2.0 注释的 play 框架,这个模型可以正常工作
@Entity
@Table(uniqueConstraints={@UniqueConstraint(columnNames = {"id_1" , "id_2"})})
public class class_name {
@Id
@GeneratedValue
public Long id;
@NotNull
public Long id_1;
@NotNull
public Long id_2;
}
Hope it helped.
希望它有所帮助。
回答by CodamRanjan
@Entity @Table(name = "stock", catalog = "mkyongdb",
uniqueConstraints = @UniqueConstraint(columnNames =
"STOCK_NAME"),@UniqueConstraint(columnNames = "STOCK_CODE") }) public
class Stock implements java.io.Serializable {
}
Unique constraints used only for creating composite key ,which will be unique.It will represent the table as primary key combined as unique.
唯一约束仅用于创建组合键,这将是唯一的。它将表作为主键组合为唯一。
回答by GlenPeterson
Note:In Kotlin the syntax for declaring the arrays in annotations uses arrayOf(...)
instead of {...}
注意:在 Kotlin 中,在注解中声明数组的语法使用arrayOf(...)
而不是{...}
@Entity
@Table(uniqueConstraints=arrayOf(UniqueConstraint(columnNames=arrayOf("book", "chapter_number"))))
class Chapter(@ManyToOne var book:Book,
@Column var chapterNumber:Int)
Note:As of Kotlin 1.2 its is possible to use the [...]
syntax so the code become much simpler
注意:从 Kotlin 1.2 开始,可以使用[...]
语法,因此代码变得更加简单
@Entity
@Table(uniqueConstraints=[UniqueConstraint(columnNames=["book", "chapter_number"])])
class Chapter(@ManyToOne var book:Book,
@Column var chapterNumber:Int)
回答by hashem yousefi
you can use @UniqueConstraint on class level, for combined primary key in a table. for example:
您可以在类级别使用@UniqueConstraint,用于表中的组合主键。例如:
@Entity
@Table(name = "PRODUCT_ATTRIBUTE", uniqueConstraints = {
@UniqueConstraint(columnNames = {"PRODUCT_ID"}) })
public class ProductAttribute{}
公共类 ProductAttribute{}
回答by R Lu
Unique annotation should be placed right above the attribute declaration. UniqueContraints go into the @Table annotation above the data class declaration. See below:
唯一的注释应该放在属性声明的正上方。UniqueContraints 进入数据类声明上方的@Table 注释。见下文:
@Entity
@Table(uniqueConstraints= arrayOf(UniqueConstraint(columnNames = arrayOf("col_1", "col_2"))))
data class Action(
@Id @GeneratedValue @Column(unique = true)
val id: Long?,
val col_1: Long?,
val col_2: Long?,
)
回答by Manjunath H M
Way1 :
方式1:
@Entity
@Table(name = "table_name", uniqueConstraints={@UniqueConstraint(columnNames = "column1"),@UniqueConstraint(columnNames = "column2")})
-- Here both Column1 and Column2 acts as unique constraints separately. Ex : if any time either the value of column1 or column2 value matches then you will get UNIQUE_CONSTRAINT Error.
-- 这里 Column1 和 Column2 分别作为唯一约束。例如:如果任何时候 column1 或 column2 的值匹配,那么您将收到 UNIQUE_CONSTRAINT 错误。
Way2 :
方式2:
@Entity
@Table(name = "table_name", uniqueConstraints={@UniqueConstraint(columnNames ={"column1","column2"})})
-- Here both column1 and column2 combined values acts as unique constraints
-- 这里 column1 和 column2 的组合值都作为唯一约束