java – Hibernate:外键的列数错误

前端之家收集整理的这篇文章主要介绍了java – Hibernate:外键的列数错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我已经定义了我的两个实体类User和Permission之间的多对多关系.用户具有username和countyId的主键组合,我的Permission表具有常规整数Id.表UserPermission具有三个外键作为其主键:username,countyId和permissionId.

由于这是一个遗留数据库,我将无法使用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.class说:

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

我认为这是要走的路,但是当我启动使用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,mappedBy = "Permissions",targetEntity = Item.class )
    private Collection

和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,CascadeType.MERGE } )
    @JoinTable(name="tblUserPermission",joinColumns = { @JoinColumn(name="Username",referencedColumnName="Username"),@JoinColumn(name="CountyId",referencedColumnName="CountyId") },inverseJoinColumns = { @JoinColumn(name="PermissionId",referencedColumnName="PermissionId") })
   private Collection

干杯

最佳答案
为了解决referencedColumnName异常

用户

@ManyToMany(cascade={CascadeType.PERSIST,cascadeType.MERGE})
private Collection

并在权限

@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

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;

}

问候,

原文链接:/spring/432522.html

猜你在找的Spring相关文章