我有两个表USERS
和USER_ROLES
。
CREATE TABLE USERS (
ID SERIAL PRIMARY KEY,
USERNAME VARCHAR(64) NOT NULL UNIQUE,
PASSWORD VARCHAR(64) NOT NULL,
ENABLED BOOLEAN NOT NULL DEFAULT TRUE,
EMAIL VARCHAR(128) NOT NULL
);
CREATE TABLE USER_ROLES (
USER_ROLE_ID SERIAL PRIMARY KEY,
USERNAME VARCHAR(64) NOT NULL UNIQUE REFERENCES USERS (USERNAME),
ROLE VARCHAR(32) NOT NULL
);
如您所见,username
是userRole
实体中Users
的引用:
@Entity
public class Users {
private int id;
private String username;
private String password;
private boolean enabled;
private String email;
private UserRoles userRoles;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="username", insertable = false, updatable = false)
public UserRoles getUserRoles() {
return userRoles;
}
public void setUserRoles(UserRoles userRoles) {
this.userRoles = userRoles;
}
@Id
@Column(name = "id", nullable = false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "username", nullable = false, length = 64)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Basic
@Column(name = "password", nullable = false, length = 64)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Basic
@Column(name = "enabled", nullable = false)
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@Basic
@Column(name = "email", nullable = false, length = 128)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Users users = (Users) o;
return id == users.id &&
enabled == users.enabled &&
Objects.equals(username, users.username) &&
Objects.equals(password, users.password) &&
Objects.equals(email, users.email);
}
@Override
public int hashCode() {
return Objects.hash(id, username, password, enabled, email);
}
}
和UserRole
实体:
@Entity
@Table(name = "user_roles")
public class UserRoles {
private int userRoleId;
private String role;
private String username;
Users users;
@Id
@Column(name = "user_role_id", nullable = false)
public int getUserRoleId() {
return userRoleId;
}
public void setUserRoleId(int userRoleId) {
this.userRoleId = userRoleId;
}
@Basic
@Column(name = "role", nullable = false, length = 32)
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
@Basic
@Column(name = "username", insertable = false, updatable = false)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@OneToOne (optional=false, mappedBy="userRoles")
public Users getUsers() { return users; }
public void setUsers(Users users) {
this.users = users;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserRoles that = (UserRoles) o;
return userRoleId == that.userRoleId &&
Objects.equals(role, that.role);
}
@Override
public int hashCode() {
return Objects.hash(userRoleId, role);
}
}
在运行时,当我执行查询:@Query("select b from Users b where b.username = :username")
时,出现以下错误:
Caused by: org.postgresql.util.PSQLException: Bad value for type int : user
at org.postgresql.jdbc.PgResultSet.toInt(PgResultSet.java:2831)
at org.postgresql.jdbc.PgResultSet.getInt(PgResultSet.java:2088)
at org.postgresql.jdbc.PgResultSet.getInt(PgResultSet.java:2502)
at com.zaxxer.hikari.pool.HikariProxyResultSet.getInt(HikariProxyResultSet.java)
我知道,出现此错误是因为外键/主键有问题,并且Hibernate尝试将整数映射到varchar
。但是我该如何解决这个问题?
数据库架构:
答案 0 :(得分:0)
我认为您在@Table(name="users")
上缺少Users.java
,而Hibernate
不知道如何映射它。
答案 1 :(得分:0)
我已经解决了我的问题。令我难过的是,原因是user_roles表中的主键类型错误。当我更改UserRoles中Users实体的getter方法的注释时,它按预期工作:
@OneToOne
@JoinColumn(name="user_role_id")
public UserEntity getUserEntity() { return userEntity; }