Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 379945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:56:28+00:00 2026-05-12T14:56:28+00:00

I have defined a many-to-many relationship between my two entity classes User and Permission.

  • 0

I have defined a many-to-many relationship between my two entity classes User and Permission. User has a primary key composite of username and countyId, and my Permission table has a regular integer Id. The table UserPermission has the three foreign keys as its primary key: username, countyId and permissionId.

Since this is a legacy database, I won’t have the opportunity to do the Right Thing(™) and make an integer primary key on User.

I’ve defined the many-to-many relationship like this in User.class:

@ManyToMany(targetEntity=Permission.class, cascade={ CascadeType.PERSIST, CascadeType.MERGE } )
@JoinTable(name="tblUserPermission",
joinColumns = { @JoinColumn(name="username"), @JoinColumn(name="countyId") },
inverseJoinColumns = { @JoinColumn(name="permissionId") })
private Collection<Permission> permissions;

Permission.class says this:

@ManyToMany( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "permissions", targetEntity = User.class )
private Collection<User> users;

I thought this was the way to go, but when I fire up my Spring context that uses Hibernate 3, I get:

Caused by: org.hibernate.AnnotationException: A Foreign key refering com.mydomain.data.entities.User from com.mydomain.data.entities.Permission has the wrong number of column. should be 1

What have I done wrong in my annotation? It should be 2, not 1.


Update:

Arthur suggested I add referencedColumnName, but that gave me a new exception:

Caused by: org.hibernate.AnnotationException: referencedColumnNames(username, countyId) of com.mydomain.data.entities.Permission.permissions referencing com.mydomain.data.entities.User not mapped to a single property

On his request, here follow the code:
Permission.Class:

package com.mydomain.data.entities;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.*;
import org.hibernate.annotations.ForeignKey;

@Entity
@Table(name = "tblPermission")
public class Permission extends PublishableEntityImpl implements Serializable, Cloneable {

    private static final long serialVersionUID = 7155322069731920447L;

    @Id
    @Column(name = "PermissionId", length = 8, nullable = false)
    private String PermissionId = "";

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "CountyId", nullable = false)
    @ForeignKey(name="FK_CountyID")
    private County county;

    @Column(name = "Permission", nullable = true)
    private Integer permission = 1;

    @ManyToMany( cascade = {CascadeType.PERSIST, CascadeType.MERGE},
             mappedBy = "Permissions",
             targetEntity = Item.class )
    private Collection<Item> items;

    @ManyToMany( cascade = {CascadeType.PERSIST, CascadeType.MERGE},
             mappedBy = "Permissions",
             targetEntity = User.class )
    private Collection<User> users;

    /** Getters and Setters **/
}

and User.class

package com.mydomain.data.entities;

import java.util.*;
import java.io.Serializable;
import javax.persistence.*;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.IndexColumn;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
@Table(name = "tblUser")
public class User extends PublishableEntityImpl implements Serializable, Cloneable {

    @Id
    @Column(name = "CountyId", nullable = false)
    private Integer countyId;

    @Id
    @Column(name = "Username", length = 25, nullable = false)
    private String username;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "CountyId", nullable = false, insertable=false, updatable=false)
    @ForeignKey(name="FK_CountyID")
    private County county;

    @Column(name = "Name", length = 50, nullable = true)
    private String name;

    @Column(name = "Password", length = 30, nullable = true)
    private String password;

    @Column(name = "Role", nullable = false)
    private Integer role;

    @ManyToMany(targetEntity=Permission.class,
            cascade={ CascadeType.PERSIST, CascadeType.MERGE } )
    @JoinTable(name="tblUserPermission",
            joinColumns = { @JoinColumn(name="Username", referencedColumnName="Username"), @JoinColumn(name="CountyId", referencedColumnName="CountyId") },
            inverseJoinColumns = { @JoinColumn(name="PermissionId", referencedColumnName="PermissionId") })
   private Collection<Permission> permissions;

    @OneToMany(fetch=FetchType.LAZY, mappedBy="county")
    @IndexColumn(name="version")
    private List<Version> versions;

    /** Getters and setters **/
}

Cheers

Nik

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:56:28+00:00Added an answer on May 12, 2026 at 2:56 pm

    In order to solve referencedColumnName exception

    In User put

    @ManyToMany(cascade={CascadeType.PERSIST, cascadeType.MERGE})
    private Collection<Permission> permissions;
    

    And in Permission

    @ManyToMany(mappedBy="permissions")
    @JoinTable(name="tblUserPermission",
     joinColumns={@JoinColumn(name="permissionId", referencedColumnName="permissionId")},
     inverseJoinColumns={
     @JoinColumn(name="username", referencedColumnName="username"),                         
     @JoinColumn(name="countyId", referencedColumnName="countyId")})
    private Collection<User> users;
    

    UserId class

    public class UserId implements Serializable {
    
        private String username;
    
        private Integer countyId;
    
        // getter's and setter's
    
        public boolean equals(Object o) {
    
            if(o == null)
                return false;
    
            if(!(o instanceof UserId))
                return false;
    
            UserId id = (UserId) o;
            if(!(getUsername().equals(id.getUsername()))
                return false;
    
            if(!(getCountyId().equals(id.getCountyId()))
                return false;
    
            return true;
        }
    
        public int hachcode() {
           // hashcode
        }
    
    }
    

    Then in User class put

    @Entity
    @Table(name="tblUser")
    @IdClass(UserId.class)
    public class User ... {
    
        @Id
        private String username;
    
        @Id
        private Integer countyId;
    
    }
    

    regards,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Perl script with many defined constants of configuration files. For example:
Routines can have parameters, that's no news. You can define as many parameters as
I have defined a custom Sharepoint list for special attributes related to a software
I have defined an interface in C++, i.e. a class containing only pure virtual
I have defined a personalizable property on a web part and I would like
I have defined a new dialog and its controls in an already existing resource
I have defined the global nls_date_format on Oracle 10.2 XE as follows: alter system
I have an interface that I have defined in C++ which now needs to
Let's say I have defined a route: routes.Add(new Route(Users/{id}, new MvcRouteHandler()) { Defaults =
Let's say we have defined a CSS class that is being applied to various

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.