春季休眠 ManyToMany 与额外的字段



我正在做一个演示项目,该项目将驱动程序和许可证连接在ManyToMany上。驱动程序可以有多个许可证,一个许可证可以连接到更多驱动程序。这不是驾驶执照。我所说的许可证与他们可以或不能运输的货物有关。这是应该的。最近,我有一个请求在此连接中添加两个额外的字段。驱动程序和执照通过表Drivers_License连接。该额外文件转到Drivers_License,即到期日期和状态发布。

这是我数据库现在的外观。

Driver                          Driver_License                   License
-----------                     -------------------             --------------------
driverID                         driverID                         licenseID
driverName                       licenseID                        licenseName
driverNumber                     expirationDate    
driverDateOfBirth                stateIssued
问题是我需要断开多对多连接

并创建两个一对一对多连接。我还需要从驱动程序 ID 和许可证 ID 制作复合密钥。

这就是我所说的例子。休眠多对多关联与联接表中的额外列示例

你能告诉我是否有一些完整的示例如何使用 spring 和休眠来完成此操作,或者您是否知道一些可以通过以经典方式使用 ManyToMany 来处理这个问题的示例?

只要您自己创建架构,就不需要将字段添加到join table。只需将它们添加到您的许可证Entity即可。这将是您似乎正在尝试做的事情的近似值。

@Entity
public class Driver {    
    @Id @GeneratedValue private Integer id;
    @OneToMany private List<License> licenses;
    ... Driver Name, Number, & DateOfBirth ...
}

@Entity
public class License {    
    @Id @GeneratedValue private Integer id;
    private String licenseName;
    // add your fields here.
    @Temporal(TemporalType.DATE)
    private Date expirationDate;
    private String issueState;
}

同样,您不想弄乱join table,它们通常由持久性提供程序自动创建。

Driver表中的licenses列表将引用一个join table,该将为一个驱动程序持有多个许可证,因此OneToMany关系。

编辑:如果您有特定的许可证并想找出它属于哪个驱动程序,比如说因为您已经按其名称查找了它,那么您应该添加对驱动程序的引用:

@ManyToOne
private Driver driver;

这将是一个ManyToOne,因为您将拥有许多涉及一个驱动程序的许可证。此关系将使用Driver中的licenses列表使用的相同Join Table。这也将创建一个循环引用,Driver引用LicenseLicense引用Driver 。您必须先创建Licenses,保存它们,创建Driver,添加许可证,保存,然后将Driver mergeLicense中。

    for(License license: licenses) {
        em.persist(license);
    }
    Driver driver = new Driver();
    driver.getLicenses().add(licenses);
    em.persist(driver);
    // merge circular dependency
    for(License license: licenses) {
        license.setDriver(driver);
        em.merge(license);
    }

好建议:您应该打开SQL输出并稍微玩一下应用程序,以了解它可以做什么以及它是如何做的。查看所有内容将有助于您更好地了解其工作原理。我通常会制作一个简单的网页,其中包含创建,打印,删除等按钮,并观看调试输出。

好的。就像我在这里承诺的那样,这是我的解决方案。尼古拉斯帮助我解决了实体组织中的一些问题,等等。

对于我的特定问题,我选择了这个:http://www.codejava.net/frameworks/hibernate/hibernate-many-to-many-association-with-extra-columns-in-join-table-example

这将解决休眠工作。我还使用DTO重新包装豆子。这里的例子有一个错误,那就是他们在复合 ID 创建中没有使用 LAZY。这就是为什么这个例子有无限循环问题。

这是我解决此问题的代码。

驱动程序实体

@OneToMany(fetch = FetchType.LAZY, mappedBy = "id.driver", cascade = CascadeType.ALL)
 private List<DriverLicense> driverLicense = new ArrayList<DriverLicense>();
 public List<DriverLicense> getDriverLicense() {
   return driverLicense;
 public void setDriverLicense(List<DriverLicense> driverLicense) {
   this.driverLicense = driverLicense;
 }

驾驶执照 ID(我的复合密钥)

@Embeddable
public class DriverLicenseID implements Serializable { 
    private static final long serialVersionUID = 1L;
    @ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.ALL) 
    @JoinColumn(name="driverID")
    private Driver driver;
    public Driver getDriver() {
        return driver;
    }
    public void setDriver(Driver driver) {
        this.driver = driver;
    }
    @ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
    private License license;
    public License getLicense() {
        return license;
    }
    public void setLicense(License license) {
        this.license = license;
    } 
}

许可证实体

@OneToMany(fetch = FetchType.LAZY, mappedBy = "id.license", cascade = CascadeType.ALL)
    private List<DriverLicense> driverLicense = new ArrayList<DriverLicense>();
    public List<DriverLicense> getDriverLicense() {
         return driverLicense;
    }
    public void setDriverLicense(List<DriverLicense> driverLicense) {
         this.driverLicense = driverLicense;
    }

谢谢尼克!你帮助我理解了实体的概念,等等。所有这些的风格都是新的。

最新更新