Hibernate AnnotationException:mappedBy引用

时间:2017-03-22 12:15:48

标签: java spring hibernate jpa

StackOverflow中有很多关于此的问题。但我仍然无法解决它。 我正在使用Hibernate编写税收系统作为我的持久层。 现在,我有两个实体类: Role.java和RolePrivilege.java。 这是Role.java

package com.taxsys.nsfw.role.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="role")
public class Role implements Serializable{

    @Id
    @GenericGenerator(name="pk_hilo", strategy="hilo")
    @GeneratedValue(generator="pk_hilo")
    @Column(name="role_id")
    private String roleId;

    @Column(name="role_name",length=32, nullable=false)
    private String roleName;

    @Column(name="state", length=1)
    private String state;

    @OneToMany(targetEntity=RolePrivilege.class, 
    mappedBy="role", cascade=CascadeType.ALL, 
    fetch=FetchType.EAGER)
    private Set<RolePrivilege> role = new HashSet<>();


    public static String ROLE_STATE_VALID = "1";
    public static String ROLE_STATE_INVALID = "0";


    public Set<RolePrivilege> getRolePrivilege() {
        return role;
    }
    public void setRolePrivilege(Set<RolePrivilege> rolePrivilege) {
        this.role = rolePrivilege;
    }
    public String getRoleId() {
        return roleId;
    }
    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }
    public String getRoleName() {
        return roleName;
    }
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public Role() {
    }
    public Role(String roleId, String roleName, String state, Set<RolePrivilege> rolePrivilege) {
        super();
        this.roleId = roleId;
        this.roleName = roleName;
        this.state = state;
        this.role = rolePrivilege;
    }
}

这是RolePrivilege.java:

package com.taxsys.nsfw.role.entity;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="role_privilege")
public class RolePrivilege implements Serializable{

    @ManyToOne(targetEntity=Role.class, fetch=FetchType.EAGER)
    @JoinColumn(name="role_id", referencedColumnName="role_id")
    @Id
    private Role role;

    @Id
    private String code;

    public RolePrivilege(Role role, String code) {
        super();
        this.role = role;
        this.code = code;
    }

    public RolePrivilege() {
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((code == null) ? 0 : code.hashCode());
        result = prime * result + ((role == null) ? 0 : role.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        RolePrivilege other = (RolePrivilege) obj;
        if (code == null) {
            if (other.code != null)
                return false;
        } else if (!code.equals(other.code))
            return false;
        if (role == null) {
            if (other.role != null)
                return false;
        } else if (!role.equals(other.role))
            return false;
        return true;
    }

}

我使用的是Hibernate3.6,spring3.0,struts2.3和tomcat7。 但是,当我启动Tomcat时,将显示错误消息:

Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.taxsys.nsfw.role.entity.RolePrivilege.role in com.taxsys.nsfw.role.entity.Role.role

看看我的代码! mappedBy =“role”。与RolePrivilege.role相同!即使我使用小写,它也不起作用!我不知道为什么。也许这是关于jar或兼容性的问题?谢谢!

2 个答案:

答案 0 :(得分:0)

删除Role private Role role;的RolePrivilege.java中的@Id并报告您仍然遇到的错误

答案 1 :(得分:0)

您可以使用@Embeddable注释在新的类对象中映射复合键,然后使用@ManyToOne映射将子对象映射到父对象。见Hibernate annotations to map one to one unidirectional association with composite primary key