4

I'm creating Spring Boot Application with JPA PostgreSQL.

When I compile my spring project, got the following error.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: eveserver.core.entity.User.name in eveserver.core.entity.Role.users

Please, help me to understand what i'm doing wrong.

This is my 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();
    }
}

and this is my tables

enter image description here

Daria
  • 347
  • 2
  • 5
  • 18
  • Possible duplicate of [mappedBy reference an unknown target entity property](https://stackoverflow.com/questions/4011472/mappedby-reference-an-unknown-target-entity-property) – crizzis Jan 17 '18 at 22:31
  • "mappedBy" should be set to `roles`, since `role` does not exist. But then the message practically tells you that now ... –  Jan 18 '18 at 07:21

1 Answers1

8

In your User class, you have a property called roles:

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

In your Role class, this:

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

should be:

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

mappedBy = "something" is saying, effectively, "within this other entity, there's a property called something that gets a list of entities of this type (the current type that you're in when you use the @ManyToMany annotation). It is not specifying a type or class name like Role.

allquixotic
  • 1,481
  • 2
  • 18
  • 37