【发布时间】:2016-10-09 21:28:32
【问题描述】:
我目前有一个完整的 Spring 应用程序,经过非常彻底的测试,并且已经在生产环境中成功运行了一年多。最近,我想在用户名字段上进行休眠电子邮件验证。用户名字段用于登录,也是我们用户的电子邮件。
当我用@Email 注释username 字段时,不再生成id(用@Id 和@Generated(strategy = GenerationType.AUTO) 注释)并保留值null。这会导致hashCode 在NullPointerException 上失败(不过这没有问题)。所以由于某种原因,由于在User.java 实体中添加了@Email 注释,因此不再生成 id。
@Entity
@Table(uniqueConstraints = @UniqueConstraint(name = "username",
columnNames = "username"))
public class User implements Serializable, UserDetails {
@GeneratedValue(strategy = GenerationType.AUTO)
@Id
private Long id;
@Column(nullable = false)
@JsonView(View.NoProfile.class)
@Email
protected String username;
@Column(nullable = false)
private String passwordHash;
@JsonIgnore
@ElementCollection(targetClass = Role.class, fetch = FetchType.EAGER)
@Enumerated(EnumType.STRING)
@CollectionTable(name = "user_role")
private Set<Role> roles;
@OneToOne(targetEntity = Profile.class, cascade = CascadeType.ALL)
@JsonView(View.Public.class)
protected Profile profile;
@JsonIgnore
private boolean accountNonExpired = true;
@JsonIgnore
private boolean accountNonLocked = true;
@JsonIgnore
private boolean credentialsNonExpired = true;
@JsonIgnore
private boolean enabled = true;
public User(String username, String passwordHash) {
this.username = username;
this.passwordHash = passwordHash;
this.profile = new Profile();
this.roles = new HashSet<>();
roles.add(Role.ROLE_USER);
}
User() { // jpa only
}
public Profile getProfile() {
return profile;
}
public Long getId() {
return id;
}
public void setPasswordHash(String passwordHash) {
this.passwordHash = passwordHash;
}
@Override
public Set<? extends GrantedAuthority> getAuthorities() {
return roles;
}
@Override
@JsonIgnore
public String getPassword() {
return passwordHash;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public boolean isAccountNonExpired() {
return accountNonExpired;
}
@Override
public boolean isAccountNonLocked() {
return accountNonLocked;
}
@Override
public boolean isCredentialsNonExpired() {
return credentialsNonExpired;
}
@Override
public boolean isEnabled() {
return enabled;
}
public void resetProfile() {
this.profile = new Profile();
}
public void addRole(Role role) {
this.roles.add(role);
}
public void setAccountNonLocked(boolean accountNonLocked) {
this.accountNonLocked = accountNonLocked;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@JsonView(View.Public.class)
public int getReference() {
return username.hashCode();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
User user = (User) o;
return username.equals(user.username) && id.equals(user.id);
}
@Override
public int hashCode() {
int result = username.hashCode();
result = 31 * result + id.hashCode();
return result;
}
}
【问题讨论】:
-
您最近没有更改您的数据库架构/实体吗?然后更新失败?
-
你有吗?
update ,或者create也不错。 -
这发生在
create和create-drop上,所以数据库应该是正确的。 -
为什么只有一个字段的类级别的 UniqueConstraint?你不应该把它放在电子邮件上,否则会令人困惑,请参阅stackoverflow.com/questions/15372654/…
-
这是一种样式选择,我们希望在文件顶部有唯一约束,以使唯一字段立即清晰。此外,不幸的是,这并不能解决手头的问题。不过感谢您的帮助。
标签: java spring hibernate email hibernate-validator