我试图通过休眠来创建我的数据库,我将我的表格和我的客户联系起来,但是当我想要关联一个到多个(一个建筑物有很多客户)时,我遇到了这个问题,
alter table CUSTOMER add constraint FK52C76FDE6049D2D3 foreign key (BUILDING_ID) references BUILDING
我使用mysql服务器和hibernate进行映射,这些是我的java.classes, 第一个是Building.java
package com.tunisiana.app.building.model;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import com.tunisiana.app.customer.model.Customer;
import com.tunisiana.app.places.model.City;
@SuppressWarnings("deprecation")
@Entity
@Table(name = "BUILDING")
@org.hibernate.annotations.Entity(dynamicUpdate = true, dynamicInsert = true)
public class Building implements Serializable {
/**
*
*/
private static final long serialVersionUID = 7781368778417866160L;
@Id
@Column(name = "BUILDING_ID", unique = true, nullable = false)
private Integer buildingId;
@Column(name = "ADRESS", nullable = false)
private String buildingAdress;
@Column(name = "X", nullable = false)
private Integer buildingX;
@Column(name = "Y", nullable = false)
private Integer buildingY;
@Column(name = "FLOOR", nullable = false)
private Integer buildingFloor;
@Column(name = "HEIGHT_OF_FLOOR", nullable = false)
private Double buildingHeightOfFloor;
@Column(name = "NUMBER_OF_FIBER", nullable = true)
private Integer buildingNumberOfFiber;
@Column(name = "Type", nullable = true)
private String buildingType;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CITY", nullable = false)
private City city;
@OneToMany( fetch = FetchType.LAZY, mappedBy = "building")
private Set<Customer> customers = new HashSet<Customer>(0);
public Building() {
super();
}
public Building(Integer buildingId) {
super();
this.buildingId = buildingId;
}
public Building(Integer buildingId, String buildingAdress,
Integer buildingX, Integer buildingY, Integer buildingFloor,
Double buildingHeightOfFloor, Integer buildingNumberOfFiber,
String buildingType) {
super();
this.buildingId = buildingId;
this.buildingAdress = buildingAdress;
this.buildingX = buildingX;
this.buildingY = buildingY;
this.buildingFloor = buildingFloor;
this.buildingHeightOfFloor = buildingHeightOfFloor;
this.buildingNumberOfFiber = buildingNumberOfFiber;
this.buildingType = buildingType;
}
public Building(Integer buildingId, String buildingAdress,
Integer buildingX, Integer buildingY, Integer buildingFloor,
Double buildingHeightOfFloor, Integer buildingNumberOfFiber,
String buildingType, City city, Set<Customer> customers) {
super();
this.buildingId = buildingId;
this.buildingAdress = buildingAdress;
this.buildingX = buildingX;
this.buildingY = buildingY;
this.buildingFloor = buildingFloor;
this.buildingHeightOfFloor = buildingHeightOfFloor;
this.buildingNumberOfFiber = buildingNumberOfFiber;
this.buildingType = buildingType;
this.city = city;
this.customers = customers;
}
public Integer getBuildingId() {
return buildingId;
}
public void setBuildingId(Integer buildingId) {
this.buildingId = buildingId;
}
public String getBuildingAdress() {
return buildingAdress;
}
public void setBuildingAdress(String buildingAdress) {
this.buildingAdress = buildingAdress;
}
public Integer getBuildingX() {
return buildingX;
}
public void setBuildingX(Integer buildingX) {
this.buildingX = buildingX;
}
public Integer getBuildingY() {
return buildingY;
}
public void setBuildingY(Integer buildingY) {
this.buildingY = buildingY;
}
public Integer getBuildingFloor() {
return buildingFloor;
}
public void setBuildingFloor(Integer buildingFloor) {
this.buildingFloor = buildingFloor;
}
public Double getBuildingHeightOfFloor() {
return buildingHeightOfFloor;
}
public void setBuildingHeightOfFloor(Double buildingHeightOfFloor) {
this.buildingHeightOfFloor = buildingHeightOfFloor;
}
public Integer getBuildingNumberOfFiber() {
return buildingNumberOfFiber;
}
public void setBuildingNumberOfFiber(Integer buildingNumberOfFiber) {
this.buildingNumberOfFiber = buildingNumberOfFiber;
}
public String getBuildingType() {
return buildingType;
}
public void setBuildingType(String buildingType) {
this.buildingType = buildingType;
}
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
public Set<Customer> getCustomers() {
return customers;
}
public void setCustomers(Set<Customer> customers) {
this.customers = customers;
}
@Override
public String toString() {
return "Building [buildingId=" + buildingId + ", buildingAdress="
+ ", buildingX=" + buildingX + ", buildingY=" + buildingY
+ ", buildingFloor=" + buildingFloor
+ ", buildingHeightOfFloor=" + buildingHeightOfFloor
+ ", buildingNumberOfFiber=" + buildingNumberOfFiber
+ ", buildingType=" + buildingType + "]";
}
}
这是我的Customer.java
package com.tunisiana.app.customer.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.tunisiana.app.building.model.Building;
@SuppressWarnings("deprecation")
@Entity
@Table(name = "CUSTOMER")
@org.hibernate.annotations.Entity(dynamicUpdate = true, dynamicInsert = true)
public class Customer implements Serializable {
/**
*
*/
private static final long serialVersionUID = 6690383671585902911L;
@Id
@Column(name = "CIN", unique = true, nullable = false)
private Integer customerCIN;
@Column(name = "CUSTOMER_NAME", nullable = false)
private String customerName;
@Column(name = "PHONE_NUMBER", nullable = false)
private String customerPhoneNumber;
@Column(name = "CUSTOMER_TYPE", nullable = false)
private String customerType;
@Column(name = "SUBSCRIPTION", nullable = true)
private String customerSubscription;
@Column(name = "NUMBER_OF_FIBER", nullable = false)
private Integer customerNumberOfFiber;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "BUILDING_ID", nullable = false)
private Building building;
public Customer() {
super();
}
public Customer(Integer customerCIN) {
super();
this.customerCIN = customerCIN;
}
public Customer(Integer customerCIN, String customerName,
String customerPhoneNumber, String customerType,
String customerSubscription, Integer customerNumberOfFiber,
Building building) {
super();
this.customerCIN = customerCIN;
this.customerName = customerName;
this.customerPhoneNumber = customerPhoneNumber;
this.customerType = customerType;
this.customerSubscription = customerSubscription;
this.customerNumberOfFiber = customerNumberOfFiber;
this.building = building;
}
public Integer getCustomerCIN() {
return customerCIN;
}
public void setCustomerCIN(Integer customerCIN) {
this.customerCIN = customerCIN;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerPhoneNumber() {
return customerPhoneNumber;
}
public void setCustomerPhoneNumber(String customerPhoneNumber) {
this.customerPhoneNumber = customerPhoneNumber;
}
public String getCustomerType() {
return customerType;
}
public void setCustomerType(String customerType) {
this.customerType = customerType;
}
public String getCustomerSubscription() {
return customerSubscription;
}
public void setCustomerSubscription(String customerSubscription) {
this.customerSubscription = customerSubscription;
}
public Integer getCustomerNumberOfFiber() {
return customerNumberOfFiber;
}
public void setCustomerNumberOfFiber(Integer customerNumberOfFiber) {
this.customerNumberOfFiber = customerNumberOfFiber;
}
public Building getBuilding() {
return building;
}
public void setBuilding(Building building) {
this.building = building;
}
}
当我跑它时,我总是遇到这个问题
juin 13, 2014 9:39:33 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: alter table CUSTOMER add constraint FK52C76FDE6049D2D3 foreign key (BUILDING_ID) references BUILDING
juin 13, 2014 9:39:33 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Cannot add foreign key constraint
我不知道问题出在哪里!你能帮帮我!!