[英]org.hibernate.AnnotationException: A Foreign key refering X from Y has the wrong number of column. should be 2
Can anyone help me with this and tell me what I'm missing. 谁能帮助我,告诉我我所缺少的。 Have gone through a number of examples and seem to have everything configured correctly but I keep getting this exception:
经历了许多示例,似乎一切都配置正确,但是我不断收到此异常:
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
I have a class called Branch
that has 1:M relationship with Customer
. 我有一个名为
Branch
的类,该类与Customer
具有1:M关系。 Customer
in turn has a 1:M relationship with Account
. Customer
又与Account
有1:M关系。
Note: Customer
also has an embeddable Address
class 注意:
Customer
还有一个嵌入式Address
类
Here is my code: Branch Class 这是我的代码: 分支类
@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 Address Class 嵌入式地址类别
@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;
}
Customer Class 客户类别
@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;
}
Account Class 帐户类别
@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;
}
Here I create the tables 在这里我创建表
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`)
);
In Account
you are saying : 在
Account
您说的是:
@ManyToOne
@JoinColumn(name = "customerId", nullable = false)
private Customer customer;
But there is not column with name customerId
(?) so you should give name to primary key of Customer
但是没有名称为
customerId
(?)的列,因此您应将名称指定为Customer
主键
try changing this in 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.