Java mappingBy 引用了一个未知的目标实体属性 - 休眠错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19895034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:08:58  来源:igfitidea点击:

mappedBy reference an unknown target entity property - hibernate error

javaspringhibernatejpamapping

提问by tommy

first, my classes:

首先,我的课程:

User

用户

package com.patpuc.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import com.patpuc.model.RolesMap;

@Entity
@Table(name = "users")
public class User {
    @Id
    @Column(name = "USER_ID", unique = true, nullable = false)
    private int user_id;
    @Column(name = "NAME", nullable = false)
    private String name;
    @Column(name = "SURNAME", unique = true, nullable = false)
    private String surname;
    @Column(name = "USERNAME_U", unique = true, nullable = false)
    private String username_u; // zamiast username
    @Column(name = "PASSWORD", unique = true, nullable = false)
    private String password;
    @Column(name = "USER_DESCRIPTION", nullable = false)
    private String userDescription;
    @Column(name = "AUTHORITY", nullable = false)
    private String authority = "ROLE_USER";
    @Column(name = "ENABLED", nullable = false)
    private int enabled;

    @OneToMany(mappedBy = "rUser")
    private List<RolesMap> rolesMap;

    public List<RolesMap> getRolesMap() {
        return rolesMap;
    }

    public void setRolesMap(List<RolesMap> rolesMap) {
        this.rolesMap = rolesMap;
    }

    /**
     * @return the user_id
     */
    public int getUser_id() {
        return user_id;
    }

    /**
     * @param user_id
     *            the user_id to set
     */
    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the surname
     */
    public String getSurname() {
        return surname;
    }

    /**
     * @param surname
     *            the surname to set
     */
    public void setSurname(String surname) {
        this.surname = surname;
    }

    /**
     * @return the username_u
     */
    public String getUsername_u() {
        return username_u;
    }

    /**
     * @param username_u
     *            the username_u to set
     */
    public void setUsername_u(String username_u) {
        this.username_u = username_u;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password
     *            the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the userDescription
     */
    public String getUserDescription() {
        return userDescription;
    }

    /**
     * @param userDescription
     *            the userDescription to set
     */
    public void setUserDescription(String userDescription) {
        this.userDescription = userDescription;
    }

    /**
     * @return the authority
     */
    public String getAuthority() {
        return authority;
    }

    /**
     * @param authority
     *            the authority to set
     */
    public void setAuthority(String authority) {
        this.authority = authority;
    }

    /**
     * @return the enabled
     */
    public int getEnabled() {
        return enabled;
    }

    /**
     * @param enabled
     *            the enabled to set
     */
    public void setEnabled(int enabled) {
        this.enabled = enabled;
    }

    @Override
    public String toString() {
        StringBuffer strBuff = new StringBuffer();
        strBuff.append("id : ").append(getUser_id());
        strBuff.append(", name : ").append(getName());
        strBuff.append(", surname : ").append(getSurname());
        return strBuff.toString();
    }
}

RolesMap.java

角色映射.java

package com.patpuc.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.patpuc.model.User;

@Entity
@Table(name = "roles_map")
public class RolesMap {
    private int rm_id;
    private String username_a;
    private String username_l;
    //private String username_u;
    private String password;
    private int role_id;

    @ManyToOne
    @JoinColumn(name="username_u", nullable=false)
    private User rUser;
   public RolesMap(){

    }
    /**
     * @return the user
     */
    public User getUser() {
        return rUser;
    }
    /**
     * @param user the user to set
     */
    public void setUser(User rUser) {
        this.rUser = rUser;
    }


    @Id
    @Column(name = "RM_ID", unique = true, nullable = false)
    public int getRmId() {
        return rm_id;
    }
    public void setRmId(int rm_id) {
        this.rm_id = rm_id;
    }

    @Column(name = "USERNAME_A", unique = true)
    public String getUsernameA() {
        return username_a;
    }
    public void setUsernameA(String username_a) {
        this.username_a = username_a;
    }

    @Column(name = "USERNAME_L", unique = true)
    public String getUsernameL() {
        return username_l;
    }
    public void setUsernameL(String username_l) {
        this.username_l = username_l;
    }



    @Column(name = "PASSWORD", unique = true, nullable = false)
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name = "ROLE_ID", unique = true, nullable = false)
    public int getRoleId() {
        return role_id;
    }
    public void setRoleId(int role_id) {
        this.role_id = role_id;
    }

}

when i try run this on server i have exception like this: Error creating bean with name 'SessionFactory' defined in ServletContext resource [/WEB-INF/classes/baseBeans.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.patpuc.model.RolesMap.users in com.patpuc.model.User.rolesMap

当我尝试在服务器上运行它时,我有这样的异常:在 ServletContext 资源 [/WEB-INF/classes/baseBeans.xml] 中定义名称为“SessionFactory”的 bean 创建时出错:调用 init 方法失败;嵌套异常是 org.hibernate.AnnotationException:mappedBy 引用了一个未知的目标实体属性:com.patpuc.model.User.rolesMap 中的 com.patpuc.model.RolesMap.users

But i don't exaclu know what i'm doing wrong. Can somebody help me fix this problem?

但我不知道我做错了什么。有人可以帮我解决这个问题吗?

采纳答案by DannyMo

By default, when you define your entity you can either use field-based or property-based access, but not both. "Access type" basically means where your JPA provider looks to determine the state of your entity. If field access is used, it looks at the instance variables. If property access is used, it looks at the getters.

默认情况下,当您定义实体时,您可以使用基于字段或基于属性的访问权限,但不能同时使用两者。“访问类型”基本上意味着您的 JPA 提供程序在何处确定您的实体的状态。如果使用字段访问,它会查看实例变量。如果使用属性访问,它会查看 getter。

In your case, you didn't explicitly define an access type, so JPA tries to figure it out by looking at where you placed your annotations. I thinkHibernate decides based on the placement of the @Idannotation. Since your @Idannotation is placed on a getter, Hibernate is using property-based access for RolesMap.

在您的情况下,您没有明确定义访问类型,因此 JPA 尝试通过查看您放置注释的位置来弄清楚。我认为Hibernate 是根据@Id注释的位置来决定的。由于您的@Id注释被放置在一个 getter 上,Hibernate 对RolesMap.

With property-based access, you do not have a property named rUserbecause you don't have a getter named getRUser().

对于基于属性的访问,您没有名为 的属性,rUser因为您没有名为 的getter getRUser()

The spec states that you should not mix your placement of annotations like that:

该规范指出,您不应像这样混合注释的位置:

All such classes in the entity hierarchy whose access type is defaulted in this way must be consistent in their placement of annotations on either fields or properties, such that a single, consistent default access type applies within the hierarchy

实体层次结构中以这种方式默认访问类型的所有此类类必须在字段或属性上的注释放置方面保持一致,以便在层次结构中应用单一、一致的默认访问类型

What I suggest doing to solve the problem:

我建议做什么来解决这个问题:

Place your annotations consistently so that there is no ambiguity (e.g. always put your annotations on the instance variables). This would result in the following changes to RolesMap:

一致地放置您的注释,以便没有歧义(例如,始终将您的注释放在实例变量上)。这将导致以下更改RolesMap

@Entity
@Table(name = "roles_map")
public class RolesMap {
    @Id
    @Column(name = "RM_ID", unique = true, nullable = false)
    private int rm_id;
    @Column(name = "USERNAME_A", unique = true)
    private String username_a;
    @Column(name = "USERNAME_L", unique = true)
    private String username_l;
    @Column(name = "PASSWORD", unique = true, nullable = false)
    private String password;
    @Column(name = "ROLE_ID", unique = true, nullable = false)
    private int role_id;
    @ManyToOne
    @JoinColumn(name="username_u", nullable=false)
    private User rUser;

    // ... constructor(s), getters/setters, etc ...
}