我有以下 SQL 模式布局:
-- postgresql82+ syntax
create table AudioTracks (
id serial primary key
, name text
, size integer
, filePath text
, additionDate timestamp default now()
);
create table Genres (
id serial primary key
, name text unique -- here is the unique constraint which troubles me
, description text
, additionDate timestamp default now()
);
create table AudioTrackGenre (
genreId integer references Genres (id) unique
, audioTrackId integer references AudioTracks (id) unique
, additionDate timestamp default now()
);
以及两个对应的表映射:
@Entity(name = "AudioTracks")
public class AudioTrack implements Serializable {
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
private String name;
@Column
private Integer size;
@Column
private String filePath;
@Column
@Temporal(TemporalType.TIMESTAMP)
private Date additionDate;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL )
@JoinTable(name = "AudioTrackGenre",
joinColumns = { @JoinColumn(name = "audioTrackId") },
inverseJoinColumns = { @JoinColumn(name = "genreId") }
)
@OrderBy("name")
private Set<Genre> genres = new HashSet<Genre>();
// setter/getter methods //
....
}
和
@Entity(name = "Genres")
public class Genre implements Serializable {
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
private String name;
@Column
private String description;
@Column
@Temporal(TemporalType.TIMESTAMP)
private Date additionDate;
@ManyToMany(mappedBy = "genres")
private Set<AudioTrack> audioTracks = new HashSet<AudioTrack>();
public Genre() { }
public Genre(String name) { this.name = name; }
// setter/getter methods //
....
}
但是,每当我尝试保存 AudioTrack 时,都会填充流派表中已经存在的流派,如下所示:
Set<Genre> genres = new HashSet<Genre>();
genres.add(new Genre("ambient"));
AudioTrack track = new AudioTrack();
track.setGenres(genres);
audioTrackService.addAudioTrack(track);
(这audioTrackService.addAudioTrack(track)
件事sessionFactory.getCurrentSession().save(track)
在最低 DAO 级别做)
我正进入(状态:
ERROR: ERROR: duplicate key value violates unique constraint "genres_name_key"
Detail: Key (name)=(ambient) already exists.
我如何告诉 Hibernate 不要尝试将已经存在的流派重新插入到Genres
级联插入表中?