Hibernate 一对多,运算符不存在(bytea = integer)
Hibernate one-to-many , operator doesn't exist ( bytea = integer )
我正在尝试使用一对多关系,但由于这个错误而卡住了:
20:17:54,621 INFO [stdout] (default task-2) Hibernate: select user0_.user_id as user_id1_1_, user0_.nick as nick2_1_, user0_.password as password3_1_ from users user0_
20:17:54,655 INFO [stdout] (default task-2) Hibernate: select userrole0_.users as users3_1_0_, userrole0_.id as id1_0_0_, userrole0_.id as id1_0_1_, userrole0_.role as role2_0_1_, userrole0_.users as users3_0_1_ from user_roles userrole0_ where userrole0_.users=?
20:17:54,657 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-2) SQL Error: 0, SQLState: 42883
20:17:54,658 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-2) BŁĄD: operator ndoesnt exist: bytea = integer
我不知道为什么会出现此异常。我不在任何地方使用 bytea
运算符。下面,我提供了源代码:
User.java
@Entity
@Table(name = "users")
public class User implements Serializable{
private static final long serialVersionUID = 2051614598479375020L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
private int id;
@Column(name = "nick")
private String nick;
@Column(name = "password")
private String password;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "users")
private Set<UserRole> userRole = new HashSet<UserRole>();
public User(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Set<UserRole> getUserRole() {
return userRole;
}
public void setUserRole(Set<UserRole> userRole) {
this.userRole = userRole;
}
}
和UserRole.java
@Entity
@Table(name = "user_roles")
public class UserRole implements Serializable{
private static final long serialVersionUID = 6881696293585198874L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
private User users;
@Column(name = "role")
private String role;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id", nullable = false)
public User getUser() {
return users;
}
public void setUser(User users) {
this.users = users;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
我认为这是一个简单的问题,但我不知道如何解决它。
UserRole
使用的是field-access,因为@Id是放在一个field上的。 user @ManyToOne
关联被放置在 属性 上。您也需要将其移动到字段级别:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;
这导致了您的问题。当 Hibernate 加载 UserRole 时,User table FK 是从未注释的 user
字段加载的,因此使用了 null。
我还将提取属性更改为 LAZY,因为 EAGER 提取是 a bad idea。
另一个小改动与 User
标识符类型有关。它应该使用 Integer
而不是 int
:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
private Integer id;
使用包装器可以让您区分瞬态实体和 attached/detached 实体。
我正在尝试使用一对多关系,但由于这个错误而卡住了:
20:17:54,621 INFO [stdout] (default task-2) Hibernate: select user0_.user_id as user_id1_1_, user0_.nick as nick2_1_, user0_.password as password3_1_ from users user0_
20:17:54,655 INFO [stdout] (default task-2) Hibernate: select userrole0_.users as users3_1_0_, userrole0_.id as id1_0_0_, userrole0_.id as id1_0_1_, userrole0_.role as role2_0_1_, userrole0_.users as users3_0_1_ from user_roles userrole0_ where userrole0_.users=?
20:17:54,657 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-2) SQL Error: 0, SQLState: 42883
20:17:54,658 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-2) BŁĄD: operator ndoesnt exist: bytea = integer
我不知道为什么会出现此异常。我不在任何地方使用 bytea
运算符。下面,我提供了源代码:
User.java
@Entity
@Table(name = "users")
public class User implements Serializable{
private static final long serialVersionUID = 2051614598479375020L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
private int id;
@Column(name = "nick")
private String nick;
@Column(name = "password")
private String password;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "users")
private Set<UserRole> userRole = new HashSet<UserRole>();
public User(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Set<UserRole> getUserRole() {
return userRole;
}
public void setUserRole(Set<UserRole> userRole) {
this.userRole = userRole;
}
}
和UserRole.java
@Entity
@Table(name = "user_roles")
public class UserRole implements Serializable{
private static final long serialVersionUID = 6881696293585198874L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
private User users;
@Column(name = "role")
private String role;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id", nullable = false)
public User getUser() {
return users;
}
public void setUser(User users) {
this.users = users;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
我认为这是一个简单的问题,但我不知道如何解决它。
UserRole
使用的是field-access,因为@Id是放在一个field上的。 user @ManyToOne
关联被放置在 属性 上。您也需要将其移动到字段级别:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;
这导致了您的问题。当 Hibernate 加载 UserRole 时,User table FK 是从未注释的 user
字段加载的,因此使用了 null。
我还将提取属性更改为 LAZY,因为 EAGER 提取是 a bad idea。
另一个小改动与 User
标识符类型有关。它应该使用 Integer
而不是 int
:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
private Integer id;
使用包装器可以让您区分瞬态实体和 attached/detached 实体。