为什么 @Column(unique = true) 不起作用?
Why @Column(unique = true) does not work?
我有以下代码:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@NotNull
@Column(name = "LOGIN", unique = true)
private String login;
@Column(name = "PASSWORD")
private String password;
@Column(name = "ROLE")
private UserRole role;
@Column(name = "E_MAIL", unique = true)
private String email;
@Convert(converter = UserStrategyConverter.class)
@Column(name = "STRATEGY")
private UserStrategy userStrategy;
@Column(name = "SUBSCRIPTION")
private Boolean subscription;
@Column(name = "MONEY")
private BigDecimal money;
我的问题:当我把这个来自邮递员的对象放在 json:
{
"firstName": "Daniel",
"lastName": "xxx",
"password": "daniel",
"role": "ROLE_USER",
"email": "test@test.pl",
"subscription": false,
"money": "1200"
}
它在实体中创建对象。问题是因为我可以在列(email
和 login
)中一次又一次地乘以这个对象而不是 unique = true
。谁能解释一下为什么?
Hibernate 将仅在 schema generation 时考虑约束 unique = true
。
在架构生成期间,将添加以下约束:
alter table User
add constraint UK_u31e1frmjp9mxf8k8tmp990i unique (email)
如果不使用架构生成,就没有使用 unique = true
的意义。
我有以下代码:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@NotNull
@Column(name = "LOGIN", unique = true)
private String login;
@Column(name = "PASSWORD")
private String password;
@Column(name = "ROLE")
private UserRole role;
@Column(name = "E_MAIL", unique = true)
private String email;
@Convert(converter = UserStrategyConverter.class)
@Column(name = "STRATEGY")
private UserStrategy userStrategy;
@Column(name = "SUBSCRIPTION")
private Boolean subscription;
@Column(name = "MONEY")
private BigDecimal money;
我的问题:当我把这个来自邮递员的对象放在 json:
{
"firstName": "Daniel",
"lastName": "xxx",
"password": "daniel",
"role": "ROLE_USER",
"email": "test@test.pl",
"subscription": false,
"money": "1200"
}
它在实体中创建对象。问题是因为我可以在列(email
和 login
)中一次又一次地乘以这个对象而不是 unique = true
。谁能解释一下为什么?
Hibernate 将仅在 schema generation 时考虑约束 unique = true
。
在架构生成期间,将添加以下约束:
alter table User
add constraint UK_u31e1frmjp9mxf8k8tmp990i unique (email)
如果不使用架构生成,就没有使用 unique = true
的意义。