gpt4 book ai didi

java - 用户详细信息和 hibernate 异常

转载 作者:行者123 更新时间:2023-12-01 10:06:48 25 4
gpt4 key购买 nike

我正在尝试启动我的网络应用程序,但出现异常:

java.util.Collection, at table: user, for columns: [org.hibernate.mapping.Column(authorities)]

我对聚合代码感到抱歉,但是这样需要更少的代码和更多的细节。

这是我的用户实体:

@Entity
@Table(name = "user")
public class User extends BaseEntity implements UserDetails {
private String email;
private String password;
private Set<Role> roles;
@Column(name = "email", nullable = false, unique = true)
public String getEmail() {return email;}
public void setEmail(String email) {this.email = email;}
@Column(name = "password", nullable = false)
public String getPassword() {return password;}
public void setPassword(String password) {this.password = password;}
@ManyToMany
@JoinTable(name = "user_role",
joinColumns = {@JoinColumn(name = "user", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "role", referencedColumnName = "name")})
public Set<Role> getRoles() { return roles; }
public void setRoles(Set<Role> role) {this.roles = role;}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {return roles;}
@Override
public String getUsername() { return email; }
@Override
public boolean isAccountNonExpired() {return true; }
@Override
public boolean isAccountNonLocked() { return true; }
@Override
public boolean isCredentialsNonExpired() { return true; }
@Override
public boolean isEnabled() { return true; }
}

它是角色实体:

@Entity
@Table(name = "Role")
public class Role implements GrantedAuthority, Serializable {
private String name;
@Id
@Column(name = "name")
public String getName() {return name;}
public void setName(String name) { this.name = name; }
@Override
public String getAuthority() {return name;}
}

最佳答案

我找到了这个问题的原因。如果我们将注释放在 getters 之上,那么 Hibernate 会自动尝试为其他未注释的 getters 创建列。有两种变体:

1) 将所有注释移至字段上方。

2) 将 @Transient 添加到所有非列的 getter 中。

关于java - 用户详细信息和 hibernate 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36388037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com