错误:AnnotationException:mappedBy引用未知的目标实体属性

时间:2018-01-17 21:58:10

标签: java spring postgresql hibernate jpa

我正在使用JPA PostgreSQL创建Spring Boot应用程序。

编译弹簧项目时,出现以下错误。

  

org.springframework.beans.factory.BeanCreationException:在类路径资源中定义名称为'entityManagerFactory'的bean时出错[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaAutoConfiguration.class]:init方法的调用失败;嵌套异常是org.hibernate.AnnotationException:mappedBy引用一个未知的目标实体属性:eveserver.core.entity.Role.users中的eveserver.core.entity.User.name

请帮助我理解我做错了什么。

这是我的 User.java

package eveserver.core.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Set;

@Entity
@Table(name = "users")
public class User implements Serializable {
    @Id
    @GeneratedValue
    private Long id;
    @Column(name = "name")
    private String username;
    @JsonIgnore
    @Column(name = "password")
    private String password;
    @Column(name = "email")
    private String email;
    private boolean enabled = false;


    @ManyToMany
    @JoinTable(name = "user_role",
            joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
    private Set<Role> roles;

    public User(){ }

    public User(String username, String password, String email) {
        this.username = username;
        this.password = password;
        this.email = email;
    }

    public User(String username, String password, String email, Set<Role> roles) {
        this.username = username;
        this.password = password;
        this.email = email;
        this.roles = roles;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    public void enable(){
        enabled = true;
    }
    public void disable(){
        enabled = false;
    }

}

Role.java

package eveserver.core.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.security.core.GrantedAuthority;

import javax.persistence.*;
import java.util.Set;

@Entity
@Table(name = "role")
public class Role  implements GrantedAuthority {
    @Id
    @GeneratedValue
    private Long id;
    private String name;

    @ManyToMany(mappedBy = "role")
    @JsonIgnore
    private Set<User> users;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object obj){
        if (obj instanceof Role){
            Role r = (Role)obj;
            if (r.getId()==this.getId()){
                return true;
            }
        }
        return false;
    }

    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

    @Override
    public String getAuthority() {
        return getName();
    }
}

这是我的表格

enter image description here

1 个答案:

答案 0 :(得分:5)

User课程中,您有一个名为roles的属性:

public Set<Role> getRoles() {
    return roles;
}

Role课程中,这个:

@ManyToMany(mappedBy = "role")
@JsonIgnore
private Set<User> users;

应该是:

@ManyToMany(mappedBy = "roles")
@JsonIgnore
private Set<User> users;

mappedBy = "something"有效地说,在这个其他实体中,有一个名为something的属性,它获取此类型的实体列表 (当您使用@ManyToMany注释时,您当前所处的类型)。 指定类型或类名称,如Role