未呈现 JPA 唯一属性
JPA unique attribute not rendered
我有一个简单的 pojo
和一个 email
属性,它在数据库中应该是唯一的 table:
@Entity
public class Customers implements Serializable {
@Id
@GeneratedValue
private Integer cID;
@Column(unique = true, nullable = false)
private String email;
@Column(nullable = false)
private String username;
@Column(nullable = false)
private String password;
//getters/setters/constructors
...
}
这是 jsf
表单 bean:
@ManagedBean
@SessionScoped
public class RegistrationBean implements Serializable {
private String username;
private String password;
private String email;
//getters/setters
public String registeration() {
Customers newCustomer = new Customers(email, username, password);
CustomerService customerService = new CustomerService();
if (customerService.persistCustomer(newCustomer) == true) {
return "Succ?faces-redirect=true";
}
return "fail?faces-redirect=true";
}
}
这是我的 form
:
(按钮动作是registeration()
)
但我尝试了两次,均等 email
s,存储成功!
为什么不显示相同 email
的任何错误?
@Column(unique = true)
导致 JPA 实现在自动创建 table 时在数据库列上创建唯一约束。
如果在创建 table 之后添加此 Annotation 参数,则它没有任何效果。您可能希望手动将唯一约束添加到该列,或者让您的 JPA 实现重新创建 table.
试试这个
@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames={"EMAIL"}))
public class Customers implements Serializable {
...
我有一个简单的 pojo
和一个 email
属性,它在数据库中应该是唯一的 table:
@Entity
public class Customers implements Serializable {
@Id
@GeneratedValue
private Integer cID;
@Column(unique = true, nullable = false)
private String email;
@Column(nullable = false)
private String username;
@Column(nullable = false)
private String password;
//getters/setters/constructors
...
}
这是 jsf
表单 bean:
@ManagedBean
@SessionScoped
public class RegistrationBean implements Serializable {
private String username;
private String password;
private String email;
//getters/setters
public String registeration() {
Customers newCustomer = new Customers(email, username, password);
CustomerService customerService = new CustomerService();
if (customerService.persistCustomer(newCustomer) == true) {
return "Succ?faces-redirect=true";
}
return "fail?faces-redirect=true";
}
}
这是我的 form
:
(按钮动作是registeration()
)
但我尝试了两次,均等 email
s,存储成功!
为什么不显示相同 email
的任何错误?
@Column(unique = true)
导致 JPA 实现在自动创建 table 时在数据库列上创建唯一约束。
如果在创建 table 之后添加此 Annotation 参数,则它没有任何效果。您可能希望手动将唯一约束添加到该列,或者让您的 JPA 实现重新创建 table.
试试这个
@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames={"EMAIL"}))
public class Customers implements Serializable {
...