SPRING MVC JPA重复键值违反了唯一约束

时间:2017-07-10 13:01:04

标签: java spring postgresql hibernate jpa

我正在开发一个Web应用程序,我正在尝试在我的数据库中保存一些数据。问题是cascaseType.ALL不与现有的孩子合并,绝对不是没有,所以我得到了标题的错误。经过一些研究后我发现可以使用SQL查询指定:ON CONFLICT DO NOTHING但我不知道如何在注释中指定它。这是我保存的代码:

Domain d1 = ImmutableDomain.builder().name("Domain1").id("1").build();
        Domain d2 = ImmutableDomain.builder().name("Domain2").id("2").build();
        Domain d3 = ImmutableDomain.builder().name("Domain3").id("3").build();
        Domain d4 = ImmutableDomain.builder().name("DevOps").id("4").build();
        Domain d5 = ImmutableDomain.builder().name("RH").id("5").build();

        Category c1 = ImmutableCategory.builder().name("Category1").id("10").addDomains(d1).build();
        Category c2 = ImmutableCategory.builder().name("Category2").id("11").addDomains(d2).build();
        Category c3 = ImmutableCategory.builder().name("Category3").id("12").addDomains(d3).build();
        Category c4 = ImmutableCategory.builder().name("BackEnd").id("13").addDomains(d4).build();
        Category c5 = ImmutableCategory.builder().name("FrontEnd").id("14").addDomains(d4).build();
        Category c6 = ImmutableCategory.builder().name("Recruitment").id("15").addDomains(d5).build();
        Category c7 = ImmutableCategory.builder().name("Administration").id("16").addDomains(d5).build();

        Skill s1 = ImmutableSkill.builder().name("Skill1").id("20").addCategories(c1).build();
        Skill s2 = ImmutableSkill.builder().name("Skill2").id("21").addCategories(c2).build();
        Skill s3 = ImmutableSkill.builder().name("Skill3").id("22").addCategories(c3).build();
        Skill s4 = ImmutableSkill.builder().name("Java").id("23").addCategories(c4).build();
        Skill s5 = ImmutableSkill.builder().name("C").id("24").addCategories(c4).build();
        Skill s6 = ImmutableSkill.builder().name("Angular").id("25").addCategories(c5).build();
        Skill s7 = ImmutableSkill.builder().name("Compatbilité").id("26").addCategories(c6).build();
        Skill s8 = ImmutableSkill.builder().name("HeadHunter").id("27").addCategories(c7).build();

        s1 = skillService.save(s1);
        s2 = skillService.save(s2);
        s3 = skillService.save(s3);
        s4 = skillService.save(s4);
        s5 = skillService.save(s5);
        s6 = skillService.save(s6);
        s7 = skillService.save(s7);
        s8 = skillService.save(s8);

这是我的网站:域名:

@Entity
@Table(name = "ATS_DOMAIN")
public class DomainEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @NotNull
    @Size(min = 1, max = 42)
    @Column(name = "domain_name", length = 42, nullable = false, unique = true)
    private String name;

类别:

@Entity
@Table(name = "ATS_CATEGORY")
public class CategoryEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @NotNull
    @Size(min = 1, max = 42)
    @Column(name = "category_name", length = 42, nullable = false, unique = true)
    private String name;

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "ATS_CATEGORY_DOMAIN", joinColumns = @JoinColumn(name = "category_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "domain_id", referencedColumnName = "id"))
    private List<DomainEntity> domains = new ArrayList<DomainEntity>();

技能:

@Entity
@Table(name = "ATS_SKILL")
public class SkillEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @NotNull
    @Size(min = 1, max = 42)
    @Column(name = "skill_name", length = 42, nullable = false, unique = true)
    private String name;

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "ATS_SKILL_CATEGORY", joinColumns = @JoinColumn(name = "skill_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "category_id", referencedColumnName = "id"))
    private List<CategoryEntity> categories = new ArrayList<CategoryEntity>();

我确切地说我尝试了cascadeType.MERGE + cascadeType.PERSIST并尝试了cascadeType.SAVE_UPDATE但它们都没有工作。

0 个答案:

没有答案