Hello, I have the following classe:
@Entity
@Table(name = "usr_user", uniqueConstraints = {
@UniqueConstraint(name = "uk_usr_login", columnNames = { "usr_login" }) })
public class User {
private long id;
private String login;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "usr_id", nullable = false, unique = true)
public long getId() {return id;}
@Column(name = "usr_login", nullable = false, unique = true, length = 255)
public String getLogin() {return login;}
public void setId(final long id) {this.id = id;}
public void setLogin(final String login) {this.login = login;}
}
Depending on whether unique=true is set in getLogin's @Column, I have the following result in the generated statement : 1. getLogin with unique = true:
create table usr_user (usr_id bigint not null, usr_login varchar(255) not null, primary key (usr_id))
alter table usr_user add constraint UK_6iua9a9q6jbsdakwv83vo40rm unique (usr_login)
2. getLogin with unique = false
create table usr_user (usr_id bigint not null, usr_login varchar(255) not null, primary key (usr_id))
alter table usr_user add constraint uk_usr_login unique (usr_login)
While the javadoc for unique explains that it is a shortcut for the UniqueConstraint annotation I would rather have Hibernate to: 1. Either fails (eg: you can't mix UniqueConstraint with unique=true) 2. Either use the name from UniqueConstraint In the example here, Hibernate somehow determined that my UniqueConstraints correspond to something already existing (otherwise I would have two constraints, UK_6iua9a9q6jbsdakwv83vo40rm and uk_usr_login). |