简体   繁体   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

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.

相关问题 org.hibernate.AnnotationException:从y引用x的外键具有错误的列数。 应该是n - org.hibernate.AnnotationException:A Foreign key refering x from y has the wrong number of column. should be n org.hibernate.AnnotationException:外键引用的列数错误。 应该是2 - org.hibernate.AnnotationException: A Foreign key refering has the wrong number of column. should be 2 AnnotationException: 引用的外键列数错误。 应该是 0 - AnnotationException: A Foreign key refering has the wrong number of column. should be 0 AnnotationException:引用dayHibernate的外键…错误的列数。 应该是3 - AnnotationException: A Foreign key refering dayHibernate … wrong number of column. should be 3 外键引用的列数错误。 应该是 2 - A Foreign key refering has the wrong number of column. should be 2 Hibernate Mapping中的错误“外键引用的列数错误。 应该是2“ - Error in Hibernate Mapping “A Foreign key refering has the wrong number of column. should be 2” hibernate4:引用的外键列数错误。 应该是 2 - hibernate4: A Foreign key refering has the wrong number of column. should be 2 Hibernate:外键的列数错误。 应该是 1 - Hibernate: a foreign key has the wrong number of column. should be 1 从MainTableEntity引用MasterDataEntity的外键具有错误的列数 - A Foreign key refering MasterDataEntity from MainTableEntity has the wrong number of column 获取 org.hibernate.AnnotationException - Getting org.hibernate.AnnotationException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM