Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 休眠一对多,操作员不';t exist(bytea=integer)_Java_Database_Hibernate_Jpa_Hibernate Mapping - Fatal编程技术网

Java 休眠一对多,操作员不';t exist(bytea=integer)

Java 休眠一对多,操作员不';t exist(bytea=integer),java,database,hibernate,jpa,hibernate-mapping,Java,Database,Hibernate,Jpa,Hibernate Mapping,我正在尝试使用一对多关系,但由于以下错误,我一直无法使用: 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) Hiber

我正在尝试使用一对多关系,但由于以下错误,我一直无法使用:

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;
    }

}

我假设这是一个简单的问题,但我不知道如何解决。

用户角色使用字段访问,因为@Id放在字段上。用户
@ManyToOne
关联被放置在属性上。您还需要将其移动到字段级别:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;
这导致了你的问题。当Hibernate加载UserRole时,用户表FK是从未注释的
User
字段加载的,因此使用了null

我还将fetch属性更改为LAZY,因为急切的抓取是无效的

另一个小变化与
用户
标识符类型有关。它应该使用
Integer
而不是
int

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
private Integer id;

使用包装器可以区分临时实体和附加/分离的实体。

这两个表上的键列是否具有不同的数据类型?请直接在数据库中检查此列的数据类型。
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
private Integer id;