ManyToMany关系不保存

ManyToMany关系不保存

本文介绍了@ManyToMany关系不保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 @ManyToMany 关系的实体:

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "buses_drivers",
        joinColumns = @JoinColumn (name = "driver_id_inner", referencedColumnName = "driver_id"),
        inverseJoinColumns = @JoinColumn (name = "bus_id_inner", referencedColumnName = "bus_id"))
private List<Bus> buses;

@ManyToMany(mappedBy = "buses", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Driver> drivers;

当执行时将 Driver code>巴士型号,一切正常。表 buses_drivers 存储这些实体的所有键。但是,当使用驱动程序保存 Bus 模型时,表不会更改。我认为 inverseJoinColmns 映射存在问题。

When execute saving Driver model with some Bus models, all ok. Tables buses_drivers store all keys those entities. But when saving Bus model with drivers, table doesn't change. I think problem with inverseJoinColmns mapping.

推荐答案

这是预期的行为。在双向多对多关联中,一方必须是反面。在你的情况下,它是 Bus 端,因为它包含
$ b

That is the expected behaviour. In a bidirectional many-to-many association one side has to be the inverse side. In your case it is the Bus side because it contains mappedBy:

这意味着 Driver 是必需的,除非关系
是单向的。 >是关联的所有者,Hibernate在维护关联时只会检查那一方。

That means that Driver is the owner of the association and Hibernate will only check that side when maintaining the association.

这篇关于@ManyToMany关系不保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!