Search code examples
javahibernateone-to-manypojo

Hibernate Exception when using mappedby attribute


I am trying to map between tables in pojo class.But I am getting exception for one attribute. Pojo Classes

Users:

@Entity
@Table(name = "Users")
public class Users implements java.io.Serializable {

    private int userId;
    private Role role;
    private Groups groupId;
    private UserType userType;
    private String userName;
    private Boolean isActive;



    public Users() {
    }

    public Users(int userId, Role role, Groups groupId ,UserType userType, String userName, boolean isActive) {
        this.userId = userId;
        this.role = role;
        this.groupId = groupId;
        this.userType = userType;
        this.userName = userName;
        this.isActive = isActive;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "UserID", unique = true, nullable = false)
    public int getUserId() {
        return this.userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "RoleID", nullable = false)
    public Role getRole() {
        return this.role;
    }

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "GroupId", nullable = false)
    public Groups getGroup() {
        return this.groupId;
    }

    public void setGroup(Groups group) {
        this.groupId = group;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "UserTypeID", nullable = false)
    public UserType getUserType() {
        return this.userType;
    }

    public void setUserType(UserType userType) {
        this.userType = userType;
    }

    @Column(name = "UserName", nullable = false)
    public String getUserName() {
        return this.userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Column(name = "IsActive", nullable = false, columnDefinition = "BIT")
    public Boolean isIsActive() {
        return this.isActive;
    }

    public void setIsActive(Boolean isActive) {
        this.isActive = isActive;
    }

    }

Groups:

@Entity
@Table(name = "Groups")
public class Groups implements java.io.Serializable{

    private int groupId;
    private String groupName;
    private String groupDesc;

    private Set<Users> users = new HashSet<Users>(0);

    public Groups (){

    }

    public Groups(int groupId, String groupName, String groupDesc){
        super();
        this.groupId = groupId;
        this.groupName = groupName;
        this.groupDesc = groupDesc;
    }

    public Groups(int groupId, String groupName, String groupDesc, Set<Users> users) {
        super();
        this.groupId = groupId;
        this.groupName = groupName;
        this.groupDesc = groupDesc;
        this.users = users;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name ="GroupID", nullable = false)
    public int getGroupId() {
        return groupId;
    }

    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }

    @Column(name = "GroupName", nullable = false)
    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    @Column(name = "GroupDesc", nullable = false)
    public String getGroupDesc() {
        return groupDesc;
    }

    public void setGroupDesc(String groupDesc) {
        this.groupDesc = groupDesc;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "groupId") //Error
    public Set<Users> getUserse() {
        return this.userse;
    }

    public void setUserse(Set<Users> users) {
        this.users = users;
    }


}

I am mapping the correct member variable of Users class. But I am getting exception like this

org.hibernate.AnnotationException: mappedBy reference an unknown target     entity property: com.project.pojo.Users.groupId in com.project.pojo.Groups.users

In users pojo I have attributes like role,usertype which are mapped without any exception.Whereas for group I am getting this exception. Can someone please help me to resolve this exception.

Thank you :)


Solution

  • There is a problem in your member variables of classes users and groups Change the variable name groupId to group of Users.pojo and change mappedby attribute to group in groups class set method.

    This will resolve your problem.