多对多Spring Data JPA关系中的额外列,只需进行最小的更改 [英] Extra columns in many-to-many Spring Data JPA Relationship with minimal changes

查看:69
本文介绍了多对多Spring Data JPA关系中的额外列,只需进行最小的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更改项目模型.如今,我们有两个具有双向多对多关系的类(这表示在关系表中),现在需要向该关系中添加其他信息.

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 (e.g. 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;
}

推荐答案

根据注释中的建议,您需要一个join实体,但无需将其公开给您的域模型的客户端:通过执行以下操作,您只会需要更改任何直接修改集合的客户端代码,以调用addXXX/removeXXX方法.没有客户代码仅知道Join实体.因此,对Java代码所做的更改应该很小.

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. No client code is only aware of Join entity. So changes to your Java code should be minor.

对于查询,您显然需要根据需要进行更改.

As for queries well you will obviously need to change these as required.

意甲

@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);   
    }
}

学科

@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);
    }
}

加入实体

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

    @ManyToOne
    private Disciplina disciplina;

    //fields for the additional data about the relationship

    //getters and setters
}

Hibernate文档建议首先避免使用 @ManyToMany 以避免此类问题.参见以下讨论:休眠最佳做法:避免多对多和异国"关系

The Hibernate docs suggest avoiding @ManyToMany in the first place to avoid such problems. See discussion at: Hibernate Best Practices: Avoiding Many-To-Many and 'exotic' relationships

这篇关于多对多Spring Data JPA关系中的额外列,只需进行最小的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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