Spring使用额外的字段休眠ManyToMany [英] Spring hibernate ManyToMany with extra fields

查看:117
本文介绍了Spring使用额外的字段休眠ManyToMany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个驱动程序和许可证连接ManyToMany的演示项目。驱动程序可以有更多的许可证,一个许可证可以连接到更多的驱动这不是驾驶执照。我所说的许可证与他们可以或不能运输的货物有关。这是它应该的方式。
最近,我有一个请求在这个连接中添加两个额外的字段。驱动程序和许可证通过表Drivers_License连接到ManyToMany。这额外的提交到Drivers_License,这是expirationDate和stateIssued。



这是我的数据库的外观。

 驱动程序Driver_License许可证
----------- ------------------- - -------------------
driverID driverID licenseID
driverName licenseID licenseName
driverNumber expirationDate
driverDateOfBirth stateIssued

问题是我需要中断ManyToMany连接并创建两个OneToMany连接。我还需要由driverID和licenseID组成的组合键。



这就是我正在谈论的例子。
休眠多对多关联连接表中的额外列示例



你能否告诉我是否有一些完整的示例如何完成此操作使用spring和hibernate,或者你知道一些可以通过古典方式使用ManyToMany的例子吗?

只要您自己创建模式,就不需要将字段添加到连接表中。只需将它们添加到您的许可证实体中即可。这是您似乎正在尝试做的近似。

  @Entity 
公共类驱动程序{
@Id @GeneratedValue私人整数ID;
@OneToMany私人清单<许可证>许可证;
...驱动程序名称,编号,& DateOfBirth ...
}

  @Entity 
public class License {
@Id @GeneratedValue private Integer id;
private String licenseName;
//在这里添加您的字段。
@Temporal(TemporalType.DATE)
私人日期expirationDate;
private String issueState;
}

再一次,您不希望乱用连接表,它们通常由持久性提供程序自动创建。


$ b 许可证列表中的驱动程序表将引用一个连接表,它将为一个驱动程序保存许多许可证,因此 OneToMany 关系。



编辑:如果你有一个特定的许可证并且想知道它属于哪个驱动程序,因为您已经查看了它的名字,那么您应该向驱动程序添加一个引用:

  @ManyToOne 
私人司机司机;

这将是 ManyToOne ,因为您会有许多引用一个驱动程序的许可证。这种关系将在 Driver 列表中使用相同的加入表,即许可证列表$ c>使用。这也将创建一个循环引用, Driver 引用许可证许可证引用 Driver 。您必须首先创建许可证,保存它们,创建驱动程序,添加许可证并保存,然后合并 驱动程序许可证中。

  for(License license:licenses){
em.persist(license);
}
驱动程序驱动程序=新驱动程序();
driver.getLicenses()。add(licenses);
em.persist(driver);
//合并循环依赖项
(许可证许可证){
license.setDriver(driver);
em.merge(license);
}

好建议:您应打开 SQL 输出并与应用程序一起玩一点点,以了解它可以做什么以及如何做。看到一切在行动将有助于你更好地感受这是如何工作的。我通常用一个简单的网页来创建一个按钮,比如创建,打印,删除等等,并观察调试输出。


I'm working on one demo project which has Driver and Licenses connected ManyToMany. Driver can have more licenses and one license can be connected to more drivers. This is not a driver licence. The license I am talking about is connected with the cargo that they can, or cannot transport. That is the way it should be. Recently, I had one request to add two extra fields in this connection. Driver and License are connected ManyToMany by table Drivers_License. That extra filed goes to Drivers_License and that is expirationDate, and stateIssued.

This is the look of my database now.

Driver                          Driver_License                   License
-----------                     -------------------             --------------------
driverID                         driverID                         licenseID
driverName                       licenseID                        licenseName
driverNumber                     expirationDate    
driverDateOfBirth                stateIssued

The problem is that I need to break ManyToMany connection and create two OneToMany connections. I also need composite key to be made from driverID and licenseID.

This is the example of that I am talking about. Hibernate Many-to-Many Association with Extra Columns in Join Table Example

Can you tell me if there is some complete example how to finish this using spring and hibernate, or maybe do you know some example that can handle this by using ManyToMany on a classical way?

解决方案

As long as you are creating the schema yourself, you don't need to add the fields to the join table. Just add them to your license Entity. This would be an approximation of what you seem to be trying to do.

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

and

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

Again, you don't want to mess with the join table, they are typically created automatically by the persistence provider.

The licenses list in the Driver table will refer to a join table that will hold many licenses for one driver, hence the OneToMany relation.

EDIT: If you have a specific license and want to find out which driver it belongs to, say because you have looked it up by its name, then you should add a reference back to the driver:

@ManyToOne
private Driver driver;

It will be a ManyToOne because you will have Many licenses which refer to One driver. This relationship will use the same Join Table that the licenses list in Driver uses. This will also create a circular reference, Driver refers to License and License refers to Driver. You will have to create the Licenses first, save them, create the Driver, add the licenses, save it, then merge the Driver into the License.

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

GOOD ADVICE: You should turn on the SQL output and play with the application a little bit to get a feel for what it can do and how it does it. Seeing everything in action will help you get a better feel about how this works. I usually make a simple web page with buttons such as create, print, delete and the like and watch the debug output.

这篇关于Spring使用额外的字段休眠ManyToMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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