简体   繁体   English

多对多Spring Data JPA关系中的额外列,变化很小

[英]Extra columns in many-to-many Spring Data JPA Relationship with minimal changes

I need to make a change in the project's model. 我需要对项目的模型进行更改。 Nowadays we have two classes with bidirectional many-to-many relationship (which implies in an relationship table) and need now to add extra informations to the relationship. 如今,我们有两个具有双向多对多关系的类(这表示在关系表中),现在需要向该关系中添加其他信息。

My question is: The only way to do it is create a class for the relationship (eg creating this one with the same name of the relationship table that already exists)? 我的问题是:唯一的方法是为关系创建一个类(例如,使用与已经存在的关系表相同的名称来创建该类)?

I'm asking it because if we need to change the above relationship in the project, the change will be very impacting, almost the whole project (seriously). 我之所以这样问是因为,如果我们需要在项目中更改上述关系,那么更改将非常严重地影响几乎整个项目。

The two classes that I'm talking about, just to make it clearer: 我正在谈论的两个类是为了使其更清楚:

@Entity
public class Serie extends AbstractEntity {
   @ManyToMany
   private List<Disciplina> disciplinas;
}

@Entity
public class Disciplina extends AbstractEntity {
   @ManyToMany(mappedBy = "disciplinas")
   private List<Serie> series;
}

As advised in the comments you need a join entity but do not need to expose this to clients of your domain model: by doing something like the below you would only need to change any client code that directly modifies the collections to call the addXXX/removeXXX methods. 如注释中所建议,您需要一个联接实体,但无需将其公开给您的域模型的客户端:通过执行以下操作,您只需更改直接修改集合的任何客户端代码即可调用addXXX / removeXXX方法。 No client code is only aware of Join entity. 没有客户代码仅知道Join实体。 So changes to your Java code should be minor. 因此,对Java代码的更改应该很小。

As for queries well you will obviously need to change these as required. 至于查询,您显然需要根据需要进行更改。

Serie 意甲

@Entity
public class Serie extends AbstractEntity {

    @OneToMany(mappedBy = "serie")
    private List<SerieDisciplina> serieDisciplinas;

    public List<Disciplina> getDisciplinas(){
            //disciplinas extracted from serieDisciplinas 
            return Collections.unmodifiableList(...); 
    }

    public void addDisciplina(Disciplina disciplina){
        SerieDisciplina sd = new SerieDisciplina();
        sd.stSerie(this);
        sd.setDisciplina(disciplina);

        serieDesciplinas.add(sd);   
    }
}

Disciplina 学科

@Entity
public class Disciplina extends AbstractEntity {

    @ManyToMany(mappedBy = "disciplina")
    private List<SerieDisciplina> serieDisciplinas;

    public List<Serie> getSeries(){
        //series extracted from serieDisciplinas
        return Collections.unmodifiableList(...); 
    }

    public void addSerie(Serie serie){
        SerieDisciplina sd = new SerieDisciplina();
        sd.stSerie(Serie);
        sd.setDisciplina(this);

        serieDesciplinas.add(sd);
    }
}

Join Entity 加入实体

@Entity 
public class SerieDisciplina{
    @ManyToOne
    private Serie serie;

    @ManyToOne
    private Disciplina disciplina;

    //fields for the additional data about the relationship

    //getters and setters
}

The Hibernate docs suggest avoiding @ManyToMany in the first place to avoid such problems. Hibernate文档建议首先避免使用@ManyToMany以避免此类问题。 See discussion at: Hibernate Best Practices: Avoiding Many-To-Many and 'exotic' relationships 请参见以下内容的讨论: 休眠最佳实践:避免多对多和“异类”关系

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM