简体   繁体   English

Spring Hibernate关系映射

[英]Spring Hibernate Relation Mapping

I'm looking for help in one of my project. 我正在我的一个项目中寻求帮助。

I'm having Company Class and Bank Class. 我有公司类和银行类。

Company Class and Bank Class are to be mapped using Many to Many Relation using Hibernate. 公司类别和银行类别将使用Hibernate使用多对多关系进行映射。

How Do I start? 我该如何开始? I'm done creating Company Module which is inserting data into table and same for the Bank. 我已经完成创建公司模块,该公司模块将数据插入表中,并且与银行相同。 But How to show the mapping between both? 但是如何显示两者之间的映射?

Flow Goes like this - Add Company -> Edit/Update -> Add Bank to the previous Company Detail -> Bank Also Add/Update -> View All, which is needed to show The list of Companies and their respective banks. 流程是这样的-添加公司->编辑/更新->将银行添加到以前的公司详细信息->银行还添加/更新->查看全部,这需要显示公司及其各自银行的列表。

I assume you use junction table so i would done it like this: 我假设您使用联结表,所以我会这样做:

Company.class 公司类

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "nameOfJunctionTable", catalog = "yourDatabaseName", joinColumns = { 
        @JoinColumn(name = "companyId", nullable = false, updatable = false) }, 
        inverseJoinColumns = { @JoinColumn(name = "bankId", 
                nullable = false, updatable = false) })
private Collection<Bank> banks;

Bank.class 银行类

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "banks")
private Collection<Company> companies;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM