代码之家  ›  专栏  ›  技术社区  ›  Sinan Samet

Hibernate给出无法确定类型错误

  •  0
  • Sinan Samet  · 技术社区  · 6 年前

    我有一个具有id和角色名称的Roles表和一个具有role\u id的users表。我想加入这些表,但我得到了错误 Could not determine type for: com.aon04.backend.models.Role, at table: users, for columns: [org.hibernate.mapping.Column(role)] 当我尝试这样做的时候。

    这是我的榜样:

    @Entity
    @Table(name = "roles")
    
    public class Role {
        @Id
        @GeneratedValue()
        @Column(name = "id")
        private int id;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "id", nullable = false)
        @JsonIgnore
        public User user;
    
        @Column(name = "name", nullable = false)
        private String name;
    
    }
    

    这是我的用户模型:

    @Entity
    @Table(name = "users")
    public class User implements Serializable {
        @Id
        @GeneratedValue()
        @Column(name = "id")
        private int id;
    
        public Role role;
    
        public String getStudentNumber() {
            return studentNumber;
        }
    
        public void setStudentNumber(String studentNumber) {
            this.studentNumber = studentNumber;
        }
    
        @Column(name = "student_number", nullable = true)
        private String studentNumber;
    
        @Column(name = "first_name", nullable = true)
        private String firstName;
    
        @Column(name = "last_name", nullable = true)
        private String lastName;
    
        @Column(name = "email", nullable = true, unique = true)
        private String email;
    
        @OneToOne(fetch = FetchType.LAZY, mappedBy = "user")
        private FinishedExam finishedExam;
    
        @JsonIgnore
        @Column(name = "password", nullable = true)
        private String password;
    
    
        @Column(name = "created_at")
        private LocalDateTime createdAt;
    
        @Column(name = "updated_at")
        private LocalDateTime updatedAt;
    
    
        @ManyToMany(fetch = FetchType.LAZY)
        @JoinTable(
                name = "event_users",
                joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
                inverseJoinColumns = @JoinColumn(name = "event_id", referencedColumnName = "id")
        )
    
        @PreUpdate
        protected void onUpdate() {
            updatedAt = LocalDateTime.now();
        }
    
        @PrePersist
        protected void onCreate() {
            createdAt = LocalDateTime.now();
            updatedAt = createdAt;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
    
        public LocalDateTime getCreatedAt() {
            return createdAt;
        }
    
        public void setCreatedAt(LocalDateTime createdAt) {
            this.createdAt = createdAt;
        }
    
        public LocalDateTime getUpdatedAt() {
            return updatedAt;
        }
    
        public void setUpdatedAt(LocalDateTime updatedAt) {
            this.updatedAt = updatedAt;
        }
    
        public Role getRole() {
            return role;
        }
    
        public void setRole(Role role) {
            this.role = role;
        }
    }
    

    我不知道我做错了什么。我想要的输出是在检索用户的JSON时,我想要显示角色为name的用户数据。

    2 回复  |  直到 6 年前
        1
  •  1
  •   HBo    6 年前

    你的模型中有一个矛盾之处:a Role 有一个 User 有着多人的关系,所以你的 使用者 应收集 角色 你没有映射你的 role 在里面 使用者 ,通过添加双向关系来实现:

    @OneToMany(mappedBy = "user")
    private List<Role> role = new ArrayList<>();
    
        2
  •  1
  •   Ajit    6 年前

    我可以看到,您具有从角色到用户的用户多对一关系。用户是否可以有多个角色? 并且您需要在用户表中添加从用户到角色的关联。当用户可以有多个角色时,它可以是@ManyToMany