JPA不会将复合constrait添加到表中

时间:2018-03-12 09:41:21

标签: java mysql hibernate dbeaver

尝试避免重复时,将相同的值插入两列的组合(" 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 个答案:

答案 0 :(得分:0)

你可以使用它,

@Table(name = "res_location", uniqueConstraints = {
  @UniqueConstraint(name = "UK_LOCATION_NAME", columnNames = { "name" }) })