当前位置: 首页 > 面试题库 >

Hibernate:外键的列数错误

姚文轩
2023-03-14
问题内容

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

由于这是一个旧数据库,因此我将没有机会进行Right Thing(™)并在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 **/
}

干杯

尼克


问题答案:

为了解决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
    }

}

然后在User类中

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

    @Id
    private String username;

    @Id
    private Integer countyId;

}

问候,



 类似资料:
  • 有人知道解决这个问题的办法吗?或者这是不可能使用Hibernate/JPA进行映射的吗? Edit:Article和Location的(简化)定义(真的没那么有趣,重要的是它们使用了复合主键):

  • 问题内容: 目标:我想在ImportJob中使用importJobId作为分配表ID的外键,这样,当我们那时具有importJobId时,那么我们只能在ID中分配ID,因为没有Job时就无法进行任何分配。 ImportJob表的复合主键为[ORGID,IMPORTJOBTYPE],正在尝试使用以下方式在hibernate状态下创建外键关系: 在Allocation.hbm.xml中无法解决,并收到

  • 我有一个实体,叫做FatRabbitCarrot: 而且很管用。现在上面的类已经替换了字段名,但是结构和我们的存储库是一样的。 然后我尝试添加一个新实体,该实体在上面的类中有一个外键。让我们把这门课叫做NutToffee。FatRabbitCarrot与这个新实体有一对一的关系,而实体本身应该有一对一的关系: 对我来说,这似乎是一个有效的类。但看起来不像。我们正在使用Java8、Hibernate

  • 问题内容: 是否有可能需要一个A列或B列具有一个值,但不是两者都具有的外键。并且A列的外键匹配表1,B列的外键匹配表2? 问题答案: 检查约束可以解决这个问题。如果这是SQL Server,则将执行以下操作:

  • 我可以这样做吗?或者有没有其他更好的方法来处理这种情况

  • 我有这个型号 而我有这个方法 但是hibernate没有设置实际地址id和注册地址id(它是OneTONE) Hibernate:插入客户(名字、姓氏、中间名、性别)值(?、、?、?)2021-03-18 14:01:58.340警告12836---[nio-8080-exec-1]o.h.发动机。jdbc。spi。SqlExceptionHelper:SQL错误:0,SQLState:23502