現在完全に動作するSpringアプリケーションがあり、非常に徹底的にテストされており、1年以上実稼働しています。最近、私はユーザー名フィールドでハイバネート電子メールの検証をしたかったのです。ユーザー名フィールドはログインに使用され、ユーザーの電子メールでもあります。私は@Email
でusername
フィールドに注釈を付けるときユーザー名に@emailアノテーションが付いている場合、SpringまたはHibernateはIDを生成しません。
、(@Id
と@Generated(strategy = GenerationType.AUTO)
で注釈さ)idはもはや生成され、値null
を保持していません。これにより、はNullPointerException
で失敗します(これは問題ありません)。したがって、何らかの理由で@Email
注釈をUser.java
エンティティに追加することで、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;
}
}
あなたはあなたのデータベーススキーマ/エンティティを最近変更していませんか?そして、更新に失敗しましたか? – Snickers3192
ありますか?更新 、またはcreateも良いです。 –
Snickers3192
これは 'create'と' create-drop'のどちらかで起こります。そのため、データベースは正しいはずです。 – martijn9612