休眠:外键的列数错误

Posted

技术标签:

【中文标题】休眠:外键的列数错误【英文标题】:Hibernate: Foreign key has the wrong number of columns 【发布时间】:2009-09-27 07:32:55 【问题描述】:

我在两个实体类 User 和 Permission 之间定义了多对多关系。用户有一个用户名和县 ID 的主键组合,我的权限表有一个常规整数 ID。表 UserPermission 具有三个外键作为主键:username、countyId 和 permissionId。

由于这是一个遗留数据库,我将没有机会做正确的事情 (™) 并在 User 上创建一个整数主键。

我在 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 是这样说的:

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

我认为这是要走的路,但是当我启动使用 Hibernate 3 的 Spring 上下文时,我得到:

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

我在注释中做错了什么?它应该是 2,而不是 1。


更新:

Arthur 建议我添加 referencedColumnName,但这给了我一个新的例外:

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

应他的要求,请遵循以下代码: 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 **/

和 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 **/

干杯

尼克

【问题讨论】:

您好,我遇到了同样的问题,但是按照您的示例,我收到此错误:引用未知实体:java.lang.String。在我的情况下,县将是字符串类型。 【参考方案1】:

为了解决referencedColumnName异常

用户输入

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

并且在许可中

@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 类

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
    


然后在用户类中放

@Entity
@Table(name="tblUser")
@IdClass(UserId.class)
public class User ... 

    @Id
    private String username;

    @Id
    private Integer countyId;


问候,

【讨论】:

所以为了清楚起见,name 是连接表中的表列,而 referencedColumnName 是实体表中的表列?无论如何,添加referencedColumnName我得到“引起:org.hibernate.AnnotationException:com.mydomain.data.entities.Permission.permissions的com.mydomain.data.entities.Permission.permissions的referencedColumnNames(username,countyId)引用com.mydomain.data.entities.User未映射到单个属性" 这很有趣,因为 'permissions' 属于 User 类,而不是 Permission 类 顺便说一句,你为什么要删除 targetEntity=Permission.class? 请稍等。如果您声明通用类型 Collection 和 Collection targetEntity 不是必需的。我会检查你的第一个疑问 权限类中的mappedBy应该是权限,而不是“P”权限 在用户类中,删除县属性中的 nullable=false

以上是关于休眠:外键的列数错误的主要内容,如果未能解决你的问题,请参考以下文章

休眠仅保存具有复合主键中的父外键的子表条目

休眠。如何将条目添加到具有来自 Hibernate 中 2 个不同表的 2 个外键的表中?

在休眠 ORM 中使用一个强制外键和一个可选外键中的列

如何确保休眠 5 在与共享主键的一对一关系中保持正确的顺序

主键的休眠@GeneratedValue null 错误

没有复合键的休眠中的多对多