Hibernate: Foreign key has the wrong number of columns

后端 未结 1 680
北海茫月
北海茫月 2021-01-05 16:31

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 ta

1条回答
  •  礼貌的吻别
    2021-01-05 17:07

    In order to solve referencedColumnName exception

    In User put

    @ManyToMany(cascade={CascadeType.PERSIST, cascadeType.MERGE})
    private Collection 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 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 讨论(0)
提交回复
热议问题