簡體   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