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

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

问题描述

我有一个 Java bean.现在,我想确保该字段应该是唯一的.

I have a Java bean. Now, I want to be sure that the field should be unique.

我正在使用以下代码:

@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?

注意:我使用的是播放框架.

推荐答案

为了确保字段值的唯一性,您可以编写

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):

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

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