繁体   English   中英

org.hibernate.AnnotationException:从Y引用X的外键具有错误的列数。 应该是2

[英]org.hibernate.AnnotationException: A Foreign key refering X from Y has the wrong number of column. should be 2

谁能帮助我,告诉我我所缺少的。 经历了许多示例,似乎一切都配置正确,但是我不断收到此异常:

org.hibernate.AnnotationException: A Foreign key refering com.bank.entity.Customer from com.bank.entity.Account has the wrong number of column. should be 2

我有一个名为Branch的类,该类与Customer具有1:M关系。 Customer又与Account有1:M关系。

注意: Customer还有一个嵌入式Address

这是我的代码: 分支类

@Entity
@Table(name = "Branch")
public class Branch extends AbstractPersistable<Long> implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "branch_Name")
private String branchName;

@OneToMany(mappedBy = "branch")
private Set<Customer> customers;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}
}

嵌入式地址类别

@Embeddable
public class Address {
@Column(name = "houseNumber", nullable = false)
private String houseNumber;

@Column(name = "streetName", nullable = false)
private String streetName;

@Column(name = "city", nullable = false)
private String city;

@Column(name = "country", nullable = false)
private String country;

@Column(name = "eirCode", nullable = false)
private String eirCode;
}

客户类别

@Entity
@Table(name = "Customer")
public class Customer extends AbstractPersistable<Long> implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "first_Name")
private String firstName;

@Column(name = "surname")
private String surName;

@Embedded
Address address;

@ManyToOne
@JoinColumn(name = "branchId", nullable = false)
private Branch branch;

@OneToMany(mappedBy = "customer")
private Set<Account> accounts;

}

帐户类别

@Entity
@Table(name = "Account")
public class Account extends AbstractPersistable<Long> implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "account_type")
private String type;

@Column(name = "interest_rate")
private double rate;

@Column(name = "account_balance")
private double balance;

@ManyToOne
@JoinColumn(name = "customerId", nullable = false)
private Customer customer;
}

在这里我创建表

CREATE TABLE IF NOT EXISTS `Branch` (
`id` BIGINT(10) NOT NULL AUTO_INCREMENT,
`branch_Name` VARCHAR(25) NOT NULL,
PRIMARY KEY (`id`)
);


CREATE TABLE IF NOT EXISTS `Customer` (
`id` BIGINT(10) NOT NULL AUTO_INCREMENT,
`first_Name` VARCHAR(25) NOT NULL,
`surname` VARCHAR(25) NOT NULL,
`houseNumber` VARCHAR(25) NOT NULL,
`streetName` VARCHAR(120) NOT NULL,
`city` VARCHAR(25) NOT NULL,
`country` VARCHAR(25) NOT NULL,
`eirCode` VARCHAR(25) NOT NULL,
`branchId` BIGINT(10)   NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_CUST_BRANCH` (`branchId`),
CONSTRAINT `FK_CUST_BRANCH` FOREIGN KEY (`branchId`) REFERENCES `Branch`   (`id`)
 );

 CREATE TABLE IF NOT EXISTS `Account` (
`id` BIGINT(10) NOT NULL AUTO_INCREMENT,
`account_type` VARCHAR(25) NOT NULL,
`interest_rate` DOUBLE NOT NULL,
`account_balance` DOUBLE NOT NULL,
`customerId` BIGINT(10)   NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_CUST_ACC` (`customerId`),
CONSTRAINT `FK_CUST_ACC` FOREIGN KEY (`customerId`) REFERENCES `Customer` (`id`)
 );

Account您说的是:

@ManyToOne
@JoinColumn(name = "customerId", nullable = false)
private Customer customer;

但是没有名称为customerId (?)的列,因此您应将名称指定为Customer主键

尝试在Customer更改此

@Entity
@Table(name = "Customer")
public class Customer extends AbstractPersistable<Long> implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="customerId")
private Long id;

...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM