当我尝试应用@OnDelete(action = OnDeleteAction.CASCADE)并使用hibernate生成表时,没有任何反应。使用属性OrhpanRemoval = true,hibernate生成表,但仍然拒绝级联删除实体。
表本身使用@OneToMany注释连接到另一个表。表格如下:
@Entity
@Table(name = "Style")
public class Style {
public Style() {
}
public Style(String nameRus, String nameEng) {
this.nameRus = nameRus;
this.nameEng = nameEng;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "style_id")
private Long id;
@Column(name = "Style_Name_rus", nullable = false)
private String nameRus;
@Column(name = "Style_Name_eng", nullable = false)
private String nameEng;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "Styles_Songs", joinColumns = @JoinColumn(name = "style_id"), inverseJoinColumns = @JoinColumn(name = "song_id"))
@OnDelete(action=OnDeleteAction.CASCADE)
private Set<Song> songs;
public Set<Song> getSongs() {
return songs;
}
public void setSongs(Set<Song> songs) {
this.songs = songs;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNameRus() {
return nameRus;
}
public void setNameRus(String nameRus) {
this.nameRus = nameRus;
}
public String getNameEng() {
return nameEng;
}
public void setNameEng(String nameEng) {
this.nameEng = nameEng;
}
@Override
public String toString() {
return this.nameRus;
}
}
表#2:
@Entity
@Table(name = "Song")
public class Song {
public Song() {
}
public Song(String name, String performer, String trackLength, Style style,
User user) {
this.name = name;
this.performer = performer;
this.style = style;
this.trackLength = trackLength;
this.user = user;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "song_id")
private Long id;
@Column(name = "Song_name", nullable=false)
private String name;
@Column(name = "Performer", nullable=false)
private String performer;
@Column(name = "Track_length", nullable=false)
private String trackLength;
@ManyToOne
@JoinColumn(name = "style_id", nullable=false)
private Style style;
@ManyToOne
@JoinColumn(name = "user_id", nullable=false)
private User user;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(
name="Commentaries",
joinColumns = @JoinColumn( name="song_id"),
inverseJoinColumns = @JoinColumn( name="comment_id")
)
private Set<Commentary> commentaries;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPerformer() {
return performer;
}
public void setPerformer(String performer) {
this.performer = performer;
}
public String getTrackLength() {
return trackLength;
}
public void setTrackLength(String trackLength) {
this.trackLength = trackLength;
}
public Style getStyle() {
return style;
}
public void setStyle(Style style) {
this.style = style;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String toString(){
return this.performer+" - "+this.name;
}
}
我错过了什么?