Reputation: 101
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
. Customer
in turn has a 1:M relationship with Account
.
Note: Customer
also has an embeddable Address
class
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`)
);
Upvotes: 0
Views: 2606
Reputation: 4573
In Account
you are saying :
@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
try changing this in Customer
@Entity
@Table(name = "Customer")
public class Customer extends AbstractPersistable<Long> implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="customerId")
private Long id;
...
}
Upvotes: 1