我有一个名为itineraryTraveller
的实体,每个itineraryTraveller
都有许多flightEntity
。当我尝试从数据库中删除itineraryTraveller
(父)时,我收到以下错误消息:
a foreign key constraint fails (`pquino01db`.`ITINERARYTRAVELLER_FLIGHTENTITY`, CONSTRAINT `FK_ITINERARYTRAVELLER_FLIGHTENTITY_flights_ID` FOREIGN KEY (`flights_ID`) REFERENCES `FLIGHTENTITY` (`ID`))"
这是我的itineraryTraveller
实体:
@Entity
public class itineraryTraveller implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<flightEntity> flights;
@Temporal(javax.persistence.TemporalType.DATE)
private Date departureDate;
private String departureLocation;
private String arrivalLocation;
private double cost;
private char status;
private ArrayList<String> stops;
private String stopPrint;
private String userName;
private int iden;
// ...
}
flightEntity
看起来像这样:
@Entity
public class flightEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Temporal(javax.persistence.TemporalType.DATE)
private Date departureDate;
private String airlineCode;
private String flightNumber;
private String departureLocation;
private String arrivalLocation;
private double businessCost;
private double economyCost;
private int numBusinessSeats;
private int numEconomySeats;
// ...
}
有人能看到问题吗?我认为我的@OneToMany
注释可能遗漏了一些东西,但我不确定是什么。我想同时删除父母和孩子。
答案 0 :(得分:1)
两个实体之间的关系是单向的,因为flightEntity
没有映射到itineraryTraveller
实体,因为@JoinColumn
上没有flightEntity
。您的问题可以有以下解决方案之一:
在@ManyToOne
上添加flightEntity
注释,如下所示:
@Entity
public class flightEntity implements Serializable {
// ....
@ManyToOne
@JoinColumn(name="<name_of_foreignkey_column>")
private itineraryTraveller traveller;
// ...
}
您必须在mappedBy
注释中添加@OneToMany
属性:
@OneToMany(mappedBy="traveller", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
从而使实体之间的关系成为双向的。
如果您已在数据库中拥有具有外键关系的表,则可以解决此问题。
在@JoinTable
注释上使用@OneToMany
注释:
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinTable(name="<join_table_name>", joinColumns=@JoinColumn("TRAVELLER_ID"), inverseJoinColumns=@JoinColumn("FLIGHT_ID"))
private List<flightEntity> flights;
(列的名称被视为示例,可以更改。)
如果数据库中没有定义外键列的表,则最后一个映射很有用,它将创建一个新表作为表之间的关联;这通常是多对多关系中的情况。
如果可以在航班实体上使用@ManyToOne
注释。这是映射一对多关系的常规方法。
最后,Java中有一些约定,即州名类应以大写字母开头。所以我会将实体名称重命名为Flight
和ItineraryTraveller
。
答案 1 :(得分:0)
请注意,在某些情况下,子对象上的@JoinColumn必须具有insertable = false和updatable = false,如下所示: @JoinColumn(name =“ user_id”,insertable = false,可更新= false)
public class User {
private List<UserRole> roles;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "user")
public List<UserRole> getRoles() {
return this.roles;
}
public void setRoles(List<UserRole> roles) {
this.roles = roles;
}
}
public class UserRole {
@ManyToOne
@JoinColumn(name = "user_id", insertable = false, updatable = false)
private User user;
}