JPA не добавляет составное ограничение в таблицу
При попытке избежать дублирования вставьте одно и то же значение в комбинацию из двух столбцов (например, "first_name" и "last_name"), я использую аннотацию
@Table(name = "author",
uniqueConstraints = {@UniqueConstraint(columnNames = {"first_name", "last_name"})})
как я прочитал в соответствующей теме. Но это не работает. Я использую JPA Hibernate, DBeaver.
Вот класс.
package domain;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Table(name = "author",
uniqueConstraints = {@UniqueConstraint(columnNames = {"first_name", "last_name"})})
@Entity
@SequenceGenerator(name = "author_id_gen", sequenceName = "author_id", initialValue = 1, allocationSize = 1)
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "author_id_gen")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
@JoinTable(name = "author_address", joinColumns = @JoinColumn(name = "author_fk_id"),
inverseJoinColumns = @JoinColumn(name = "address_fk_id"))
private List<Address> address;
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
@JoinColumn(name = "author_fk_id")
private List<Phone> phones;
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
@JoinColumn(name = "author_fk_id")
private List<EMail> eMails;
public Author(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.address = new ArrayList<>();
this.phones = new ArrayList<>();
this.eMails = new ArrayList<>();
}
public Author() {
}
public void addAddress(Address address) {
this.address.add(address);
}
public void addPhone(Phone phone) {
this.phones.add(phone);
}
public void addEMail(EMail eMail) {
this.eMails.add(eMail);
}
}
1 ответ
Вы можете использовать это,
@Table(name = "res_location", uniqueConstraints = {
@UniqueConstraint(name = "UK_LOCATION_NAME", columnNames = { "name" }) })