[英]Handling hibernate Bidirectional relationships One to Many
I have two tables inside of my legacy application. 我的旧应用程序内部有两个表。
Host
and Tables
. Host
和Tables
。 Table Host
has a foreign key relationship (Tables_fk) column
with Tables
. 表
Host
与Tables
具有外键关系(Tables_fk) column
。 In my code I have a method as below. 在我的代码中,我有如下方法。
public class Tables {
public void addHosts(Host host) {
getHostsCollection().add(host);
}
}
Now I am splitting my database and Tables
will go into a separate database. 现在,我将拆分数据库,
Tables
将进入一个单独的数据库。 I am now dropping the column Tables_fk
from Host
and adding a column Table_ID
there which I will write from my application. 现在,我从
Host
删除Tables_fk
列,并在其中添加Table_ID
列,我将从应用程序中写入该列。 Now since I am writing the Table_ID
to the host table from the application, I am guessing there is no need for the addHosts(Host host)
mwthod to exist as of now. 现在,由于我是从应用程序将
Table_ID
写入主机表的,因此我猜到现在addHosts(Host host)
不需要存在addHosts(Host host)
mHost。
Am I right? 我对吗? Also what does Hibernate add with that method since the
Tables
table does not have any foreign key relationship with Host
. Hibernate还使用该方法添加了什么,因为
Tables
表与Host
没有任何外键关系。
First of all if you drop a table within your database, hibernate will no longer work for ManyToOne relationship (Table entity) so: 首先,如果您在数据库中删除表,休眠将不再适用于ManyToOne关系(表实体),因此:
you need to remove your ManyToOne annotation within your Table class and then change it to @Transient or remove the hosts variable entirely together with getters and setters
您需要删除Table类中的ManyToOne批注,然后将其更改为@Transient或与getter和setter一起完全删除hosts变量
And if you do so, your relationship is not Bi-Directional anymore , that relationship will become Uni-Directional and it works fine and you still can fetch all of your "tables" column rows! 而且,如果您这样做了,则您的关系不再是双向的 ,该关系将变为单向的,并且可以正常工作,并且您仍然可以获取所有“表”列的行!
Take note adding column is different with adding a Foreign Key and the section you have mentioned about dropping Tables_fk creates ambiguity
请注意,添加列与添加外键不同,您提到的有关删除Tables_fk的部分会产生歧义
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.