将@ManyToMany关联表与额外列映射 [英] Mapping @ManyToMany association table with extra columns

查看:158
本文介绍了将@ManyToMany关联表与额外列映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库包含3个表: person document peson_document 。 Person和Document具有多对多关系,并与 person_document 表连接,该表包含添加列。
这是我的映射:

My database contains 3 tables: person, document and peson_document. Person and Document have many-to-many relationship and are joined with the person_document table which contains addition columns. This is my mapping:

class Person {
    @Cascade(CascadeType.ALL)
    @OneToMany(mappedBy = "compositePK.person", orphanRemoval = true)
    Set<PersonDocument> personDocuments;
}

class Document {
    @OneToMany(mappedBy = "compositePK.document")
    Set<PersonDocument> personDocuments;
}

class PersonDocument {

    @EmbeddedId
    private CompositePK compositePK;

    @Column(name = "person_origin_id")
    private String personID;

    @ManyToOne(fetch = FetchType.LAZY)
    private Provider provider;    

    @Embeddable
    public static class CompositePK implements Serializable {

        @ManyToOne(fetch = FetchType.LAZY)
        private Person person;

        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "documents_guid")
        private Document document;
    }

在我的代码中,我正在尝试保存这样的实体:

In my code I'm trying save this entities like this:

Set<PersonDocument> pds = new HashSet<>();
Document doc = new Document();
//set doc, set person to new PersonDocument and add once on pds set.
person.getPersonDocuments().addAll(pds);
personRepository.save(person);

问题是hibernate没有保存它并抛出异常: insert或更新表违反外键约束 - 因为在文档表中没有记录。那么为什么hibernate在保存 person_document 之前不会持久存在文档以及如何解决这个问题?

The problem is that hibernate doesn't save it and throws exception: insert or update on table violates foreign key constraint - because in document table there is no record. So why hibernate don't persist document before saving person_document and how solve this problem?

推荐答案

试试这个:

public class Person {
    @OneToMany(mappedBy = "person", orphanRemoval = true, cascade= CascadeType.ALL)
    Set<PersonDocument> personDocuments;
}


class Document {
    @OneToMany(mappedBy = "document")
    Set<PersonDocument> personDocuments;
}


public class PersonDocument {

    @EmbeddedId
    private CompositePK compositePK;

    @MapsId("person")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "person_origin_id")
    private Person person;

    @MapsId("document")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "documents_guid")
    private Document document;

    @ManyToOne(fetch = FetchType.LAZY)
    private Provider provider;    
}


@Embeddable
public class CompositePK implements Serializable {

    //these fields should have the same type as the ID field of the corresponding entities
    //assuming Long but you have ommitted the ID fields

    private Long person;

    private Long document;

    //implement equals() and hashcode() as per the JPA spec
}


//you must always set both side of the relationship
Person person = new Person();
Document document = new Document();
PersonDocument pd = new PersonDocument();
pd.setPerson(person);
pd.setDocument(document);
person.getPersonDocuments.add(pd);//assumes initialized

这篇关于将@ManyToMany关联表与额外列映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆