JPA三个不同的表外键在一个表映射

时间:2017-01-06 22:41:48

标签: mysql hibernate jpa spring-data-jpa

我有一个表有三个不同的表外键。我想映射它,但它不是一起插入数据。这是表格细节

id
analysis_type_id
product_id
edition_id

product_idproduct表的外键。 edition_idedition表的外键。 analysis_type_idanalysis_type表的外键。

这是类映射

    @Entity
    @Table(name = "t_analysis_type", catalog = "")
    public class TAnalysisTypeEntity implements Serializable {
    private Collection<TProductEntity> tProductEntities;
    private Collection<TProductEditionEntity> tProductEditionEntities;

    @ManyToMany( fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    @JoinTable(
            name = "product_edition_analysis_type_map",
            joinColumns = @JoinColumn(name = "analysis_type_id"),
            inverseJoinColumns = @JoinColumn(name = "product_id")
    )
    public Collection<TProductEntity> gettProductEntities() {
        return tProductEntities;
    }

    public void settProductEntities(Collection<TProductEntity> tProductEntities) {
        this.tProductEntities = tProductEntities;
    }

    @ManyToMany( fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    @JoinTable(
            name = "product_edition_analysis_type_map",
            joinColumns = @JoinColumn(name = "analysis_type_id"),
            inverseJoinColumns = @JoinColumn(name = "edition_id")
    )
    public Collection<TProductEditionEntity> gettProductEditionEntities() {
        return tProductEditionEntities;
    }
    public void settProductEditionEntities(Collection<TProductEditionEntity> tProductEditionEntities) {
        this.tProductEditionEntities = tProductEditionEntities;
    }
}

产品类

@Entity
@Table(name = "t_product", catalog = "")
public class TProductEntity implements Serializable {

    @ManyToMany(mappedBy = "tProductEntities")
    public Collection<TAnalysisTypeEntity> gettAnalysisTypeEntities() {
        return tAnalysisTypeEntities;
    }

    public void settAnalysisTypeEntities(Collection<TAnalysisTypeEntity> tAnalysisTypeEntities) {
        this.tAnalysisTypeEntities = tAnalysisTypeEntities;
    }

}

产品版类

@Entity
@Table(name = "t_product_edition", catalog = "")
public class TProductEditionEntity implements Serializable {
    private Collection<TAnalysisTypeEntity> tAnalysisTypeEntities;

    @ManyToMany(mappedBy = "tProductEditionEntities")
    public Collection<TAnalysisTypeEntity> gettAnalysisTypeEntities() {
        return tAnalysisTypeEntities;
    }

    public void settAnalysisTypeEntities(Collection<TAnalysisTypeEntity> tAnalysisTypeEntities) {
        this.tAnalysisTypeEntities = tAnalysisTypeEntities;
    }
}

现在问题是当我尝试将带有版本的产品插入分析类型时,它会向表中添加两行。代码如下。它增加了两行

TAnalysisTypeEntity tAnalysisTypeEntity = tAnalysisTypeRepository.findOne(10001);
        TProductEntity tProductEntity = tProductRepository.findOne(1);

        List<TProductEntity> tProductEntities = new ArrayList<>();
        tProductEntities.add(tProductEntity);

        TProductEditionEntity tProductEditionEntity = tProductEditionRepository.findOne(1);
        List<TProductEditionEntity> tProductEditionEntities = new ArrayList<>();
        tProductEditionEntities.add(tProductEditionEntity);

        tAnalysisTypeEntity.settProductEditionEntities(tProductEditionEntities);
        tAnalysisTypeEntity.settProductEntities(tProductEntities);
        tAnalysisTypeRepository.save(tAnalysisTypeEntity);

行:

'5','10001',NULL,'1'
'6','10001','1',NULL

我想要这样的数据

'5','10001','1','1'

0 个答案:

没有答案