简体   繁体   English

一对一处理休眠双向关系

[英]Handling hibernate Bidirectional relationships One to Many

I have two tables inside of my legacy application. 我的旧应用程序内部有两个表。

Host and Tables . HostTables Table Host has a foreign key relationship (Tables_fk) column with Tables . HostTables具有外键关系(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.

相关问题 Hibernate中的双向一对一关系 - Bidirectional one-to-one relationships in Hibernate Dropwizard / Hibernate:持久存在具有双向一对多关系的对象会给出错误“列不允许为NULL” - Dropwizard/Hibernate: persisting objects with bidirectional one-many relationships gives error “NULL not allowed for column” 映射一对多休眠实体 - Mapping a bidirectional One-to-Many Hibernate Entity 带有组合键的休眠双向一对多 - Hibernate Bidirectional one-to-many with composite key 休眠双向一对多插入重复项 - hibernate bidirectional one-to-many inserts duplicates 使用泛型将多对多休眠 - Hibernate Many to one bidirectional using generics Hibernate:为多对一关系生成的属性? - Hibernate: generated properties for many-to-one relationships? Hibernate:通过示例查询和多对一关系 - Hibernate: queries by example and many-to-one relationships 正确的多个一对多关系的休眠实现 - Correct hibernate implementation for multiple one to many relationships 错误处理 JPA/Hibernate 实体双向关系的循环引用/依赖关系与 Jackson `JsonIgnoreProperties` - Errors Handling Circular References/Dependencies of JPA/Hibernate Entity Bidirectional Relationships with Jackson `JsonIgnoreProperties`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM