Java中的@UniqueConstraint注释 [英] @UniqueConstraint annotation in Java

查看:191
本文介绍了Java中的@UniqueConstraint注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java bean。现在,我想确保该字段应该是唯一的。
我使用以下代码:

I have a Java bean. Now, I want to be sure that the field should be unique. I am using following code:

@UniqueConstraint(columnNames={"username"})
public String username;

但是我收到了一些错误:

But am getting some error:

@UniqueConstraint is dissallowed for this location

什么是正确的方法使用唯一约束?

Whats the proper way to use unique constraints?

注意:我正在使用Play框架。

Note: I am using play framework.

推荐答案

要确保字段值是唯一的,您可以编写

To ensure a field value is unique you can write

@Column(unique=true)
String username;

@UniqueConstraint注释用于在表级注释多个唯一键,这就是为什么你得到一个将其应用于字段时出错。

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.

参考文献(JPA TopLink):

References (JPA TopLink):

  • @UniqueConstraint
  • @Column

这篇关于Java中的@UniqueConstraint注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆